无论应用是后台还是前台,均未收到FCM通知

时间:2019-07-10 09:49:24

标签: android firebase-cloud-messaging postman android-notifications

使用firebase通知组件或邮递员发送通知后,我没有收到通知,尽管我都收到了成功的反馈

  • 我已关注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");
        }


    }
}

使用邮递员   body {

 "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()中的日志将被记录,但是什么也没得到

1 个答案:

答案 0 :(得分:0)

看起来像您没有互联网许可。 在清单中添加以下权限

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

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