android - 待定 Intent 未按预期工作

标签 android android-intent firebase-cloud-messaging google-cloud-functions android-notifications

我在处理 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
     }
    };

我的 list 看起来像这样:

  <?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 通知负载中获取额外信息的原因,这是您从服务器发送的。

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

相关文章:

java无法从同一个类中获取静态变量的值

android反序列化对象?

firebase - 在 Firebase 消息传递中使用特定语言发送给所有用户

android - Jetpack 撰写 : Crash when adding view to window manager

android - android项目中两个库的重复类

安卓 Intent .ACTION_VIEW

android - FCM 返回错误 : NotRegistered after the first message

ios - 几周后 Firebase 推送通知不起作用

android - itextpdf-itextg Android Lint 错误

android - 如何使用 android 远程输入将额外数据传递给广播接收器