代码之家  ›  专栏  ›  技术社区  ›  Kmel

是否可以仅使用数据有效负载FCM通知启动特定活动?

  •  1
  • Kmel  · 技术社区  · 8 年前

    使用 Firebase Cloud Function ,当用户回复另一个时,我会自动发送通知。发送通知后,用户可以打开它,并应显示显示对话的活动。如果像下面这样仅使用数据作为有效负载,则无法执行click\u操作并打开相应的活动。这样定义有效负载(并在onMessageReceived中获取数据)不起作用:

    const payload = {
      data : {
        post : xxx,
        comment : xxx,
        from : xxx,
        to : xxxx,
        action_click : "open_activity_B"
      }
    };
    

    有没有一种方法可以在不添加通知的情况下实现我想要的?

    谢谢你的帮助

    编辑:

    更多的信息来解释我为什么这样做。我想创建如下通知(在前台和后台):

    enter image description here

    我只通过使用我提供给您的方法实现了这一点,但没有使用click\u操作。如果我使用通知负载,我无法显示大图标(通知末尾的化身)。 此外,在前台,我的图标应用程序显示在通知中,但在后台,我有一个默认图标。。。

    我的代码如下:

    if (remoteMessage.getData().size() > 0) {
            post_id = remoteMessage.getData().get("post");
            comment_id = remoteMessage.getData().get("comment");
            originatorUid = remoteMessage.getData().get("originatorUid");
            image = remoteMessage.getData().get("image");
    
            if (/* Check if data needs to be processed by long running 
    job */ true) {
                // For long-running tasks (10 seconds or more) use 
    Firebase Job Dispatcher.
                scheduleJob();
            } else {
                // Handle message within 10 seconds
                handleNow();
            }
    
        }
    
        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            title = remoteMessage.getNotification().getTitle();
            body = remoteMessage.getNotification().getBody();
            clickaction = remoteMessage.getNotification().getClickAction();
            icon = remoteMessage.getNotification().getIcon();
        }
    

    然后在onMessageReceived中:

    Intent intent=new Intent(clickaction);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("post_id", post_id);
        intent.putExtra("comment_id", comment_id);
        intent.putExtra("originatorUid", originatorUid);
        pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
    
        String channelId = getString(R.string.app_name);
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.mipmap.myicon)
                        .setLargeIcon(image)
                        .setContentTitle(title)
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);
    
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
    
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Alex Mamo    8 年前

    根据 official documentaion :

    具有通知和数据负载的消息,包括后台和 前景。 在这种情况下,通知将发送到设备系统托盘,数据有效负载将在extras中发送 您的 启动器活动 。

    启动器活动在 AndroidManifest.xml 使用类别的文件 LAUNCHER 如下所述:

    <activity
        android:name="com.example.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    

    您可以更改默认行为覆盖并指定另一个活动。在您的 message notification data ,添加名为 click_action 使用动作字符串的值。然后在 AndroidManifest。xml 与操作匹配的文件,如以下示例所示:

    {
      "to": "tgFvOPQLccSe:EDE90N.........5Tg",
      "notification": {
        "title": "My Message",
        "body": "Hello Kmel!",
        "click_action": "com.example.MY_NEW_ACTIVITY"
      },
      "data": {
        "score": "111"
      }
    }
    

    如下定义意图过滤器:

    <activity android:name=".MyFcmNotificationActivity">
        <intent-filter>
            <action android:name="com.example.MY_NEW_ACTIVITY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    

    但请记住,数据有效负载在收到消息时不会传递给活动,而是在用户单击通知时传递。

    推荐文章