【问题标题】:How to Open Activity even when app is in background when receive firebase notification收到firebase通知时,即使应用程序处于后台,如何打开活动
【发布时间】:2020-06-23 09:21:36
【问题描述】:

我想在接收到 Firebase 通知时打开接听电话或拒绝接听电话。但是当应用程序在前台但当应用程序在后台通知日志显示但从不调用活动时,它会打开活动。

公共类 FirebaseNotificationService 扩展 FirebaseMessagingService {

private static final String TAG = "Firebase";
private int remedyNotificationID = 0;
NotificationManager notificationManagerRemedy;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e(TAG, "Notification Received ");
    Map<String, String> data = null;
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Notification Received: " + remoteMessage.getData());
        data = remoteMessage.getData();
    }
    if (data != null) {
        Intent intent = new Intent(getApplicationContext(), IncomingVideoCallActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("user_id", data.get("user_id"));
        bundle.putString("user_full_name", data.get("user_full_name"));
        intent.putExtras(bundle);
        startActivity(intent);
    }
}

}

【问题讨论】:

  • 后台场景使用点击动作处理
  • 我不明白。你能解释一下吗

标签: android firebase-cloud-messaging


【解决方案1】:

消息有数据消息和通知两种类型 消息。处理数据消息 在 onMessageReceived 中,应用程序是在前台还是后台。数据消息是类型 传统上与 GCM 一起使用。通知消息仅在应用程序时在 onMessageReceived 中接收 在前台。当应用程序在后台时,会显示自动生成的通知。 当用户点击通知时,他们将返回到应用程序。包含两个通知的消息 和数据有效载荷被视为通知消息。 Firebase 控制台始终发送通知 消息。

示例代码 :- 您需要像这样在通知负载中指定 click_action

$noti = array
    (
    'icon' => 'new',
    'title' => 'title',
    'body' => 'new msg',
    'click_action' => 'open IncomingVideoCallActivity'
); 

现在在manifest 文件中的IncomingVideoCallActivity 活动标签中执行此操作

         <activity
               android:name=".IncomingVideoCallActivity">
                <intent-filter>
                    <action android:name="open IncomingVideoCallActivity" /> // should be same as in click action
                   <category android:name="android.intent.category.DEFAULT"/>
               </intent-filter>
        </activity>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多