待定 Intent 未按预期工作

Pending Intent are not Working as Expected

我在处理 Notification 和 pending Intent 时遇到了很大的麻烦。我正在尝试使用发送消息的适当 user_details 打开聊天 activity。这就是为什么在 Firebase Function 上我传递了 from_user_id ,这是发送消息的人。我在 FCM 中获得了正确的日志,但是当我收到聊天通知并打开它时,它会打开 activity,但没有任何用户名和消息。它使用默认值打开 activity 的新实例。

  @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);


    String notification_title = remoteMessage.getNotification().getTitle();
    String notification_message = remoteMessage.getNotification().getBody();

    String click_action = remoteMessage.getNotification().getClickAction();


    String from_user_id = remoteMessage.getData().get("from_user_id");

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this, CHANNEL_ID)
                        .setSmallIcon(R.drawable.chitchat_icon)
                        .setContentTitle(notification_title)
                        .setAutoCancel(true)
                        .setContentText(notification_message);


        Intent resultIntent = new Intent(click_action);
        resultIntent.putExtra("user_id", from_user_id);


        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        mBuilder.setContentIntent(resultPendingIntent);


    int mNotificationId = (int) System.currentTimeMillis();

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mNotificationManager.createNotificationChannel(mChannel);
        mBuilder.setChannelId(CHANNEL_ID);
    }

    mNotificationManager.notify(mNotificationId,mBuilder.build());
}

消息负载:

  const payload = {
     notification: {
      title: userName,
      body: message,
      icon: "default",
      click_action : "com.example.chitchat_TARGET_MESSAGE_NOTIFICATION"
     },
     data : {
      from_user_id : from_user_id
     }
    };

我的清单是这样的:

  <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.chitchat">
...        
       <application
            android:name=".ChitChat"
            android:allowBackup="true"
            android:icon="@drawable/chitchat_icon"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".GroupChatActivity"></activity>
            <activity android:name=".CallActivity" />
            <activity
                android:name=".ChatActivity"
                android:parentActivityName=".MainActivity">
                <intent-filter>
                    <action android:name="com.example.chitchat_TARGET_MESSAGE_NOTIFICATION" />

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

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

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
                android:theme="@style/Base.Theme.AppCompat" />

            <meta-data
                android:name="com.google.firebase.default_notification_channel_id"
                android:value="fcm_default_channel" /> <!-- adding -->
            <service android:name=".FirebaseMessagingService">
                <intent-filter>
                    <action android:name="com.google.firebase.MESSAGING_EVENT" />
                </intent-filter>
            </service>
        </application>
        ...

    </manifest>

我不知道我是否添加了一些不同的功能,如 LifeCycleEvent Listners 和用于注册的 EmailVerification 会产生问题。我也无法记录我不知道为什么的问题。 请,适当的建议。 谢谢

首先在 onMessageReceived 中更改 resultIntent.putExtra,例如:

resultIntent.putExtra("from_user_id", from_user_id);

然后在您的 ChatActivity 中获取 user_id,如下所示:

String user_id = getIntent().getStringExtra("from_user_id") 

希望这能解决您的问题。

或更改您的通知负载:

const payload = {
     notification: {
      title: userName,
      body: message,
      icon: "default",
      click_action : "com.example.chitchat_TARGET_MESSAGE_NOTIFICATION"
     },
     data : {
      user_id : from_user_id
     }
    };

并在 onMessageReceived 内更改:

String from_user_id = remoteMessage.getData().get("from_user_id");

String from_user_id = remoteMessage.getData().get("user_id");

原因:在后台期间,System 生成通知而不执行您在 onMessageReceived 中的代码.这就是为什么它会在从通知有效负载from_user_id 中获取额外内容的原因,这是您从服务器发送的。