I have configured push notifications with firebase in my app, so far so good, I receive notifications with the app on and off, when it is open and something arrives I ask for the body to do certain actions for example open another activity like the following example
public class MiFirebaseMessagingService extends FirebaseMessagingService {
public static final String TAG = "NOTICIAS";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String from = remoteMessage.getFrom();
Log.d(TAG, "Mensaje recibido de: " + from);
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Notificación: " + remoteMessage.getNotification().getBody());
mostrarNotificacion(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Data: " + remoteMessage.getData());
}
}
private void mostrarNotificacion(String title, String body) {
Intent intent = null;
if(body.equals("Login")){
intent = new Intent(this, Login.class);
}
if(body.equals("AppIntro")){
intent = new Intent(this, AppIntroGalery.class);
}
if(body.equals("Menu")){
intent = new Intent(this, TabsActivity.class);
}
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
} }
what if I don't know how it could be achieved is for when it is closed this code doesn't seem to work it only opens it but it doesn't go to the different activities any ideas?
Try to put a flag to the intent
intent = new Intent(this, SplashInicio.class);
intent.putExtra("idScreen", 12);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
splash class:
@Override
protected void onNewIntent(Intent intent) {
String idScreen = getIntent().getExtras().getString("idScreen");
if(idScreen.equals("12")){
startActivity(new Intent(this, Login.class));
}
// clean intent for new Push Notification Data.
super.onNewIntent(null);
}
also without results, to be more specific I need that when the app is closed and I get a notification, it depends on what I get in the message to do a certain action, the best thing would be to be able to open an activity and send it a flag or not
I add Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="odontosys.com.odontosys">
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/pagoexitosoxxxhdpi"
android:label="@string/app_name"
android:roundIcon="@mipmap/audi"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".MiFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MiFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/ic_launcher_round" />
<activity android:name=".SplashInicio"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Login"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ListadoClientes"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden">
</activity>
<activity android:name=".AppIntroGalery"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden">
</activity>
<activity
android:name=".TabsActivity"
android:label="@string/title_activity_tabs"
android:theme="@style/AppTheme.NoActionBar">
</activity>
</application>
Bruno, the reception of data in your class that extends from
FirebaseMessagingService
and that specifically occurs inonMessageReceived()
is understood to be to generate a notification, this does not matter if the application is open or closed.As for your method it can work without any problem, you do NOT need to specify any type of FLAG or use onNewIntent() :
For it to work properly you have to take into account these points:
1) You need to specify your activities inside your AndroidManifest.xml file
2) The parameter
body
must specifically contain the text that you indicate in the comparison:3) The most important point is that using FCM it is received in a RemoteMessage and not a Bundle. You are showing the notification if
remoteMessage.getNotification()
:but it does not open the
intent
towards the Activity because the value ofremoteMessage.getNotification().getBody()
, is not "Login", "AppIntro" or "Menu", you should check this point.It is also important to change the code of the notification because if you received one previously that did not have
body
the same code that you have with value 0, then this would cause it not to open any Activity, I suggest you create a method to obtain a random value that will serve as code:and define this code in the intent:
In this case, only the first notification will open one
Activity
in the application since, as I mentioned, the condition is that itbody
has a value of "Login", "AppIntro" or "Menu", if you open one that does not meet this condition, it will simply open from the beginning.I tell you that if you have the app closed it will not work that way, well I already tried it, what the firebase documentation recommends and that if it worked for me, is to send in the message data, the activity that I am looking for to open to When clicking on the notification and of course, the background color so that the icon does not look all white, I send them from php in this way, but it would apply to any other way of requesting it:
The subject in question is the click_action which is where you indicate which activity you want to open when you click on the notification. The color is used for when the icon remains blank, because when it is in the background or closed, it does not take the configuration of the firebase listener.
This works perfect when the app is closed or in the background.
Saludos
Bien una de las formas en hacer lo que tu quieres es obtener los datos desde la actividad principal que es la primera en abrirse. cuando la app esta cerrada por completo y te llega una notificación al hacer clic en la notificación esta ya te abre el intent principal con datos como Extras, entonces esos extras deberás obtenerlos de la siguiente forma en tu
onResume
Now you will ask yourself, for example, why I sent to get
action
, well as I asked you if you were using the Firebase console in the Custom Data section you can putaction
as the Key and then the values would be the ones you want to reviewLogin
AppIntro
etc. This answer is more based on my experience receiving Push notification in FireBase With the App closed and doing an action according to what is received in the notification.As an annex to my answer I send you the link of how you send the data when using FCM: Receive Messages