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

  @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");


原因:在后台期间,系统会生成通知,而不会在onMessageReceived内部执行您的代码。这就是为什么它将多余的东西从通知有效载荷中获取的原因,它是从服务器发送的from_user_id。

关于android - 待定 Intent 未按预期工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58425546/

10-12 01:51