【问题标题】:Pending Intent are not Working as Expected待定意图未按预期工作
【发布时间】:2023-04-08 09:03:02
【问题描述】:

我在通知和待处理的 Intent 方面遇到了很大的麻烦。我正在尝试使用发送消息的适当 user_details 打开聊天活动。这就是为什么在 Firebase 函数上我传递了 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 是否产生了问题。我也无法记录我不知道为什么的问题。 请,适当的建议。 谢谢

【问题讨论】:

  • 当这种情况发生时,在 BackgroundForeground 期间?
  • 背景。基本上我已经限制它只在用户状态在线时接收通知,即当应用程序被最小化或关闭时。在这两种情况下,我都无法打开正确的活动。
  • 检查我的答案并告诉我。谢谢
  • 我已经做到了,只有我的键值是 user_id 并且我在聊天活动中获得了 user_id

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


【解决方案1】:

首先在 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)中放置额外的内容,即您从服务器发送的。

【讨论】:

  • 我也将 user_id 作为 Intent Extra 传递给其他活动
  • 那么你必须改变payload:data : { from_user_id : from_user_id }
  • 数据:{ user_id : from_user_id }
  • 它确实有效。但是当应用程序在后台时,它只打开聊天活动,当我按下应用程序关闭而不是转到主要活动时。
  • 那是另一回事,你必须在 ChatActivity onBackPressed 中配置它
猜你喜欢
  • 2019-04-19
  • 2020-03-04
  • 2013-12-10
  • 2019-04-07
  • 1970-01-01
  • 2022-01-22
  • 2018-12-02
  • 1970-01-01
  • 2019-08-31
相关资源
最近更新 更多