通过推送通知打开不同的活动

时间:2018-07-28 13:10:12

标签: android push-notification

我的应用程序中有4个活动,可以从推送通知中打开。我正在发送click_action =活动名称表单有效载荷,并且已在清单文件中设置活动名称。但是,当我发送推送通知时,所有click_action提及都只打开一个活动。

<activity
        android:name=".notification_1">
        <intent-filter>
            <action android:name="notification_1" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

<activity
        android:name=".notification_2">
        <intent-filter>
            <action android:name="notification_2" />

            <category android:name="android.intent.category.DEFAULT"     />
        </intent-filter>
    </activity>

当我发送click_action = notification_2时,我想打开notification_2 和notification_1相同。

我的通知类如下

NotificationMessagingService extends FirebaseMessagingService {


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {


    String title = remoteMessage.getNotification().getTitle();
    String message = remoteMessage.getNotification().getBody();
    String clickAction = remoteMessage.getNotification().getClickAction();

    Intent intent = new Intent(clickAction);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
    notificationBuilder.setContentTitle(title);
    notificationBuilder.setContentText(message);
    notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    notificationBuilder.setSound(alarmSound);
    notificationBuilder.setVibrate(new long[] { 250, 250, 250, 250, 250});
    notificationBuilder.setBadgeIconType(R.mipmap.ic_launcher);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert notificationManager != null;
    notificationManager.notify(1,notificationBuilder.build());

    super.onMessageReceived(remoteMessage);

}
}

1 个答案:

答案 0 :(得分:0)

1)您可以手动检查点击操作字符串并更改意图。

示例

    if(clickAction.equals("notification_1"){ 
        intent = new Intent(this,notification1.class);
     }
    else if(clickAction.equals("notification_2")
    {
        intent = new Intent(this,notification2.class);
    }
    else .........

2)您可以发送自定义数据有效载荷,该载荷可以在remoteMessage.getData()HashMap中捕获,并进行if if else检查以创建不同的意图。

https://firebase.google.com/docs/cloud-messaging/send-with-console#about_message_fields_in_the_notifications_composer

  

数据自定义数据您定义的键/值对。这些作为数据有效载荷交付给应用程序处理。