首页 > 解决方案 > 无论应用程序在后台还是前台,都未收到 FCM 通知

问题描述

使用firebase通知组合器或邮递员发送通知后没有收到通知,尽管我从两者都获得了成功的反馈

AndroiManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.fcmpushnotification">
    <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>

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

    </application>

</manifest>

FCM服务等级

public class MyFirebaseMessageService extends FirebaseMessagingService {

      @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        Log.d("FIREBASETOKEN", "testing if method is called ");

        //checking if , there is any notification
        if (remoteMessage.getNotification()!=null) {
            String title = remoteMessage.getNotification().getTitle();
            String message = remoteMessage.getNotification().getBody();
            NotificationHelper helper = new NotificationHelper();
            helper.setBody(message); helper.setTitle(title);
            NotificationHelper.displayNotification(getApplicationContext(), helper);
            Log.d("FIREBASETOKEN", "notification is not empty");

        }else {
         //Toast.makeText( , "notification is empty" , Toast.LENGTH_LONG).show();
            Log.d("FIREBASETOKEN", "notification is empty");
        }


    }
}

使用邮递员 身体 {

 "to" : "cI1AZOAfrv8:APA91bFQUcKD6gGIfCHplDXCMCFvgk9K2b-jwsN2g_3omDMM0_y_twT0SpqWYzoBWhzWYhcsedbNZCfsygBqDVT5nECOL9sBM7mKFn_4oxry7H8P_rxVvvPRCTXWitxw4V2xd2Gkbmtb",
 "collapse_key" : "type_a",
 "notification" : {
     "body" : "Body of Your Notification",
     "title": "Title of Your Notification"
 }
}

回复

{
    "multicast_id": 6756762729932992317,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "0:1562751631028566%77e482e477e482e4"
        }
    ]
}

我希望收到通知和onMessageReceived() 要记录的日志,但什么也没得到

标签: androidfirebase-cloud-messagingpostmanandroid-notifications

解决方案


看起来你在互联网许可下失踪了。向您的清单添加以下权限

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

并且互联网必须在您的设备上打开。


推荐阅读