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>