处理通知点击时的特定活动导航

时间:2018-10-18 07:46:32

标签: android android-intent push-notification firebase-cloud-messaging android-pendingintent

我到处搜索,但没有发现有效。

我想在单击通知后开始特定的活动,但是该应用程序继续从SplashActivity启动。

这是我的代码...尝试解决这个问题,我开始变得疯狂...

Firebase实施

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());
}

}

我的清单

 <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>

我不明白问题出在我如何在FirebaseService或清单文件中调用活动。

1 个答案:

答案 0 :(得分:1)

使用此

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());
}