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

问题描述 投票:0回答:2

应用程序关闭时,我在使用 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. 向您要打开的活动添加意图过滤器:

    <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. 将“click_action="your-action-name"”字段添加到您的 FCM 消息中。

Android 会自动打开 Activity,其操作与 click_action 相同

android push-notification firebase-cloud-messaging
2个回答
1
投票

您将需要使用 Android 深层链接。

您的通知 (FCM) 应该包含一些数据的有效负载(消息 ID 完全取决于您的设计),用于您想要在本例中使用的聊天活动的给定屏幕。

聊天活动需要在清单中声明主机和架构,以下是深层链接的说明和代码:

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

快乐编码。


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

希望对你有用

© www.soinside.com 2019 - 2024. All rights reserved.