当前位置: 首页 > 知识库问答 >
问题:

如何在Android中通过推送通知打开特定活动

师野
2023-03-14

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

点击推送通知将打开主活动。在其中,我可以从intent获得一种类型的通知,并打开另一个活动:

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

在我看来,这种方法既肮脏又不方便。是否可以指定单击推送通知时将打开的活动?

编辑:解决问题的步骤:

>

  • 为要打开的活动添加意图过滤器:

    <activity android:name=".messages.chatroom.ChatRoomActivity">
        <intent-filter>
            <action android:name="*your-action-name*"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
    

    向FCM消息添加click_action="you-action-name"字段。

    Android将自动打开活动与行动相同click_action

  • 共有2个答案

    郑俊美
    2023-03-14

    首先你应该有特定的有效负载与您的通知在您的情况下它应该是一个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);
              }
    

    希望这对你有用

    唐增
    2023-03-14

    你需要使用Android深度链接。

    你的通知(FCM)应该有一些数据(消息ID取决于你的设计)的有效载荷,用于你想要使用的给定屏幕,在这种情况下,你的聊天活动。

    聊天活动需要在清单中声明主机和模式,下面是深度链接的说明和代码:

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

    快乐编码。

     类似资料:
    • 我看到了一个Firebase控制台的代码:如何为通知指定click_action,我使用了该通知,但在初始化变量CLS时出现了一个错误。我试图通过定义CLS=NULL来解析,以清除错误。它无法使用click_action打开我指定的活动 拜托,我有没有搞错什么?我怎么才能让它起作用?

    • > firebaseService类 公共类FirebaseMessagingService扩展了com.google.firebase.messaging。FirebaseMessagingService{private static int count=0;@Override public void onMessageReceived(@NonNull RemoteMessage Remote

    • 我正在工作的应用程序中,我被要求显示通知。对于通知,我使用的是FireBase云消息(FCM)。我能够得到通知,当应用程序在后台。 2.)MyFirebaseInstanceIDService 这是我在MyFirebaseMessagingService类中为onMessageReceived()方法编写的代码示例。

    • 我正在我的应用程序中实施第三部分服务(FreshChat,一种允许您将帮助台集成到应用程序或网站中的服务),除了聊天的推送通知之外,一切都很好。Freshchat通过Firebase工作(Firebase推送通知不会出现问题),但Fresat通知不起作用。我遵循了newchat的官方留档,但有已弃用的方法,绝对不起作用(我尝试了很多) 这是Firebase云消息传递代码: 以及FreshChat文

    • 如何通过Azure从我的UWP-App向不同设备上的应用程序的其他实例发送推送通知? 以下是注册设备以接收推送的说明。(这是可行的)第二部分是关于如何在控制台应用程序上发送推送(这也是可行的)https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-windows-store-dotnet-get-star