Reputation: 53
I searched everywhere, but nothing I found works.
I want to start a specific Activity after notification click, but the app continues to start from SplashActivity.
Here is my code...I'm starting becoming crazy trying solving this...
Firebase Implementation
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
private NotificationManager mNotificationManager;
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d(TAG, "Refreshed token: " + s);
sendResistrationToServer(s);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage == null) {
Log.d(TAG, "Notification message was empty");
return;
}
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
showNotification(remoteMessage);
}
private void sendResistrationToServer(String s) {
Preferences.set(Constants.Firebase.FIREBASE_APP_TOKEN, s);
}
private void showNotification(RemoteMessage remoteMessage) {
//Activity to be open
Intent i = new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Titolo")
.setContentText("testo")
.setAutoCancel(true)
.setContentIntent(pendingIntent);
mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notificationBuilder.build());
}
}
My Manifest
<application
android:name=".my.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<activity android:name=".my.ui.activity.SplashScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".my.ui.activity.MainActivity"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan" />
<activity
android:name=".my.ui.activity.ProfileActivity"
android:label="@string/title_activity_modal_login" />
<activity
android:name=".my.ui.activity.LanguageSettingActivity"
android:label="@string/title_activity_modal_login" />
<activity
android:name=".my.ui.activity.NotificationActivity"
android:label="@string/title_activity_modal_login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".my.base.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
I don't understand if the problem is how I call the activity inside FirebaseService or into the manifest file.
Upvotes: 0
Views: 299
Reputation: 1802
Use this
private void showNotification(RemoteMessage remoteMessage) {
//Activity to be open
Intent i = new Intent(this, NotificationActivity.class);
i .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Titolo")
.setContentText("testo")
.setAutoCancel(true)
.setContentIntent(pendingIntent);
mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notificationBuilder.build());
}
Upvotes: 1