如何在Android&FCM中从推送通知中打开特定活动?

3

当应用程序关闭时,我使用FCM收到有关聊天中新消息的通知。当我点击它时,我想打开聊天活动,但却打开了主活动。

单击推送通知将打开主要活动。在其中,我可以从意图中获取通知类型并打开另一个活动:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    processNotification()
}

private fun processNotification() {
    val notificationCategory = intent?.extras?.getString("category") ?: return
    if (notificationCategory == "new_message") {
        startMessagingActivity()
        finish()
    }
}

这种方法对我来说显得不太干净且不方便。是否可以指定在点击推送通知时将要打开的活动?

编辑:解决步骤:

  1. Add a intent-filter to the activity you want to open:

    <activity android:name=".messages.chatroom.ChatRoomActivity">
        <intent-filter>
            <action android:name="*your-action-name*"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
    
  2. Add 'click_action="your-action-name"' field to your FCM message.

Android会自动打开与click_action相同的操作活动。

2个回答

1
您需要使用Android深度链接。
您的通知(FCM)应该有一些数据的有效负载(消息ID取决于您的设计),用于您想在此处使用的屏幕,例如聊天活动。
聊天活动将需要在清单中声明主机和模式,在此处是Deep Linking的说明和代码:

https://developer.android.com/training/app-links/deep-linking

愉快的编程。


谢谢,我之前不知道深度链接。 - Mykhailo Plotnikov

-1

首先,您应该在通知中具有特定的有效负载,在您的情况下,它应该是特定聊天的ID,然后您可以使用该ID来执行您想要做的操作,使用此代码

 type = remoteMessage.getData().get("type");
if(type.equals("1")) { // here you can just detremine the activity you want to open
              Intent intent;
              PendingIntent pendingIntent;
              NotificationCompat.Builder builder;
              if (notifManager == null) {
                  notifManager = (NotificationManager) getSystemService
                          (Context.NOTIFICATION_SERVICE);
              }

              intent = new Intent (this, Admin_page.class); // here chose the activity
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                  int importance = NotificationManager.IMPORTANCE_HIGH;
                  if (mChannel == null) {
                      NotificationChannel mChannel = new NotificationChannel
                              ("0", title, importance);
                      mChannel.setDescription (body);
                      mChannel.enableVibration (true);
                      mChannel.setVibrationPattern (new long[]
                              {100, 200, 300, 400, 500, 400, 300, 200, 400});
                      notifManager.createNotificationChannel (mChannel);
                  }
                  builder = new NotificationCompat.Builder (this, "0");

                  intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP |
                          Intent.FLAG_ACTIVITY_SINGLE_TOP);
                  pendingIntent = PendingIntent.getActivity (this, 0, intent, 0);
                  builder.setContentTitle (title)  // flare_icon_30
                          .setSmallIcon(R.drawable.logo_app).setTicker(title).setWhen(0)
                          .setContentText (body)  // required
                          .setDefaults (Notification.DEFAULT_ALL)
                          .setAutoCancel (true)
                          .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.logo_app))
                          .setContentIntent (pendingIntent)
                          .setSound (RingtoneManager.getDefaultUri
                                  (RingtoneManager.TYPE_NOTIFICATION))
                          .setVibrate (new long[]{100, 200, 300, 400,
                                  500, 400, 300, 200, 400});
              } else {

                  builder = new NotificationCompat.Builder (this);

                  intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP |
                          Intent.FLAG_ACTIVITY_SINGLE_TOP);
                  pendingIntent = PendingIntent.getActivity (this, 0, intent, 0);
                  builder.setContentTitle (title)
                          .setSmallIcon(R.drawable.logo_app).setTicker(title).setWhen(0)
                          .setContentText (body)  // required
                          .setDefaults (Notification.DEFAULT_ALL)
                          .setAutoCancel (true)
                          .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.logo_app))

                          .setContentIntent (pendingIntent)
                          .setSound (RingtoneManager.getDefaultUri
                                  (RingtoneManager.TYPE_NOTIFICATION))
                          .setVibrate (new long[]{100, 200, 300, 400, 500,
                                  400, 300, 200, 400})
                          .setPriority (Notification.PRIORITY_HIGH);
              } // else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              Notification notification = builder.build ();
              notifManager.notify((int)date.getTime(), notification);
          }

希望对你有帮助


1
不是我发送通知。应用程序已关闭,因此 onMessageReceived 没有被调用。 - Mykhailo Plotnikov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接