【问题标题】:FCM notification not received whether the app in background or foreground无论应用程序在后台还是前台,都未收到 FCM 通知
【发布时间】:2019-07-10 09:49:24
【问题描述】:

在使用 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");
        }


    }
}

使用邮递员 身体 {

 "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() 中的日志,但没有收到任何消息

【问题讨论】:

  • 检查你输入的token是否正确。
  • 我使用了从task.getResult().getToken(); 获得的令牌,但我仍然没有收到通知
  • 检查互联网和互联网状态的权限
  • 我已经添加了权限,但仍然没有在手机上收到通知
  • 互联网权限应该启用,从应用程序设置检查

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


【解决方案1】:

您似乎缺少互联网权限。 向您的清单添加以下权限

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2020-06-21
    • 2017-10-27
    相关资源
    最近更新 更多