【问题标题】:Firebase Notifications not being received when app is in background even if data payload is passed即使传递了数据有效负载,当应用程序处于后台时也不会收到 Firebase 通知
【发布时间】:2017-12-27 10:25:10
【问题描述】:

现在我知道,如果我发送数据消息,即使应用程序在后台,也会收到通知。但即使在发送数据消息之后,我也没有收到任何通知。

我正在使用 Postman 进行测试,这是我的 Postman 请求正文:

{
 "to" : (device token),

 "data" : {
     "body" : "great match!",
     "title" : "Portugal vs. Denmark",
     "content_available" : true,
     "priority" : "high",
     "sound": "default",
     "time_to_live":"2419200"
  } 
}

这是我的onMessageReceived() 功能:

public void onMessageReceived(RemoteMessage remoteMessage) {

    Map<String,String> data=remoteMessage.getData();
    String title=data.get("title");
    String body=data.get("body");

    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            sendPushNotification(title,body);

        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }
}

这就是我的sendPushNotification() 函数的样子:

private void sendPushNotification(String title,String message) {

    try {

        String imageUrl="";
        MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        if(imageUrl.equals("null")){
            mNotificationManager.showSmallNotification(title, message, intent);
            mNotificationManager.playNotificationSound();
        }else{
            mNotificationManager.showBigNotification(title, message, imageUrl, intent);
            mNotificationManager.playNotificationSound();
        }

    } catch (Exception e) {
        Log.e(TAG, "Json Exception: " + e.getMessage());
    }
}

我在关注堆栈溢出问题:How to handle notification when app in background in Firebase

P.S.:当应用程序在前台时,收到通知就好了。

【问题讨论】:

  • 感谢您的回复!但我只使用数据有效负载..问题也需要使用通知有效负载。此外,我无法在任何地方看到通知。
  • 好的,澄清一下你想要datanotifcation?还是只有一个?
  • 我想在应用程序在后台时收到通知...我真的很困惑...我看到了其他类似问题的答案,他们说我需要使用数据有效负载只有这样,当应用程序在后台时才会收到通知..你能帮忙吗?

标签: java android firebase firebase-notifications


【解决方案1】:

正如您在我的帖子中看到的,对于特定 ID,您应该使用 "registration_ids": ["{device-token}","{device2-token}","{device3-token}"] 键和值而不是 ”to”。那是主题

【讨论】:

  • 嘿!感谢您的浏览和回答!我尝试了您的建议,但问题是,即使设备令牌正确,我也会收到一条错误消息“InvalidRegistration”
  • 哦,我忘了问:你用的是什么设备?这很重要
  • 我使用的是联想 Vibe K5
  • 嘿伙计!非常感谢您的帮助!通知在 Moto、Redmi、Samsung 和大多数其他设备上运行良好,除了我的 Lenovo Vibe K5 和其他非常易于管理的 Android Oreo 设备。再次非常感谢!你真的帮了很多忙! :D
  • 嘿!我很高兴能帮上忙。我希望你能在未来为你的手机解决这个问题。这应该是 FCM 或 Lenovo 实施的问题。让我知道我是否可以通过其他方式提供帮助。快乐的代码!
【解决方案2】:

您需要在通知负载中定义click_action

"notification"{
     "click_action":"YOUR_ACTIVITY_NAME"
  }

同样在您的清单中编辑活动定义,如下所示

<activity
            android:name="com.demo.xyzActivity"
            android:windowSoftInputMode="stateHidden">
            <intent-filter>
                <action android:name=".xyzActivity" /> //This should be one set into payload
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

这将在应用处于后台时打开您的活动。

【讨论】:

    【解决方案3】:

    您是否将通知负载与数据负载一起使用?由于函数找到通知有效负载,它直接将其发送到通知托盘,因此数据有效负载被忽略。 改变

    JSONObject json = new JSONObject(remoteMessage.getData().toString());
    

    Map<String, String> bundleData = remoteMessage.getData();
    

    这将删除 try catch 并让您知道它导致的异常。

    【讨论】:

      【解决方案4】:

      创建广播接收器

      在 Manifest.xml 中声明

          <uses-permission android:name="android.permission.WAKE_LOCK" />
      
          <receiver
                      android:name=".OnBootBroadcastReceiver">
                      <intent-filter>
                          <action android:name="android.intent.action.BOOT_COMPLETED" />
                      </intent-filter>
          </receiver>
      

      创建 OnBootBroadcastReceiver.java 文件并在其中调用 FCM 服务。

      import android.content.BroadcastReceiver;
      import android.content.Context;
      import android.content.Intent;
      
      public class OnBootBroadcastReceiver extends BroadcastReceiver {
          @Override
          public void onReceive(Context context, Intent intent) {
              Intent i = new Intent("com.example.FirebaseMessagingReceiveService");
              i.setClass(context, FirebaseMessagingReceiveService.class);
              context.startService(i);
      
          }
      }
      

      【讨论】:

      • FirebaseMessagingReceiveService.class 是什么?
      • FirebaseMessagingReceiveService 是从 FCM 接收任何消息时调用的类,在您的情况下其名称可能不同。例如,扩展 FirebaseMessagingService 并具有 onMessageReceived() 方法的类。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多