Firebase 通知已通过 CURL 和 Firebase 控制台成功发送,但未在设备上收到

Firebase Notification successfully sent via CURL and Firebase console but not received on the device

我正在尝试使用旧版应用服务器协议向我的 android 设备发送推送通知。根据响应,通知已成功发送,但我的 android 设备上没有收到任何信息。 我还尝试使用 firebase 控制台发送通知,它也显示已成功发送,但我还是没有收到任何东西。

这个问题在下面的回答中讨论过,但是我没有犯这里的回答中提到的错误。

这是我提出的要求

 curl -X POST 
-H "Authorization:key=MyAuthorizationKey" 
-H "Content-Type: application/json" 
-d '{ "message": { "notification": { 
                       "title": "FCM Message", 
                        "body": "This is an FCM Message", },
      "to": "MyDeviceToken"}
}' https://fcm.googleapis.com/fcm/send 

这是我得到的回复

{"multicast_id":8835070472349844293,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1555418431449800%06445bacf9fd7ecd"}]}

发出此请求时,我的应用 运行 未在前台运行。因此,通知应该由 google 服务传送和显示,但这并没有发生。

这是我的清单文件代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.srishti.elderly">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SignUp"
            android:label="SignUp" />
        <activity
            android:name=".SignIn"
            android:label="SignIn" />

        <service android:name=".MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>

        </service>

        <activity android:name=".Home"
            android:label="Home"></activity>

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_launcher_background"/>
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent"/>

    </application>

</manifest>

这是我的 onMessageReceived 函数

 public void onMessageReceived(RemoteMessage remoteMessage) {


        Log.d(TAG, "on message received");

        final Context context = this;
        Intent intent = new Intent(this, MainActivity.class);
        final PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);


        Notification noti = new Notification.Builder(this)
                .setContentTitle("FTest")
                .setContentText("Firebabse notification test")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentIntent(pIntent)
                .build();


        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, noti);
    }

更新:

这是新的 json 我尝试发送它:

curl -X POST 
-H "Authorization:key=Authorizationkey" 
-H "Content-Type: application/json" 
-d '{ 
     "data": { "title": "FCM Message", "body": "This is an FCM Message" }, 
     "to": "devicetoken"
}' https://fcm.googleapis.com/fcm/send 

更新: 当我 运行 在另一个它工作的设备上时,我的设备出现了问题。谢谢。

问题是您发送的消息仅在应用程序在前台时有效,要使其在应用程序在后台时有效,您应该使用数据,试试这种形式:

{
  "data": {
      "title":"FCM Message", 
      "Body":"This is an FCM Message"            
  }, 
 "to" : "devicetoken"
}  

解释:

  • notification - 仅当您的应用程序处于 background/killed 或已交付时,消息才会直接进入 Android 的通知托盘到 onMessageReceived() 方法(如果您的应用程序在前台)。

  • 数据有效负载 - 无论您的应用程序是在前台还是后台还是被终止,这些消息都将始终传送到 onMessageReceived() 方法。