【问题标题】:Firebase cloud messaging notification not received by device设备未收到 Firebase 云消息通知
【发布时间】:2016-09-17 23:51:53
【问题描述】:

我在使用 FireBase 云消息传递时遇到问题,我从设备获取令牌并通过 Google Firebase 通知控制台发送通知测试,但是,通知从未被记录或推送到 android 虚拟设备。 FCM 的文档几乎就是我在下面的代码,而在使用 firebase 时,您还需要做些什么来获得推送通知。我已经完成了文档中指定的所有设置信息(build.gradle 添加、安装 google play 服务等),但仍然没有生成消息。在推送“I/FA:未找到标签管理器,因此不会使用”的通知后,我确实收到了一次性错误,但这是唯一输出的数据,我没有找到与标签相关的任何内容谷歌上的经理要求和 FCM。我没有收到到 logcat 或设备的推送通知的代码有什么问题?请让我知道任何有帮助的进一步信息。 谢谢。

NotificationGenie.java

public class NotificationGenie extends FirebaseMessagingService {

private static final String TAG = "Firebase_MSG";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO(developer): Handle FCM messages here.
    // If the application is in the foreground handle both data and notification messages here.
    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
    sendNotification(remoteMessage.getNotification().getBody());
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}

private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, PostLoginActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.tf2spyprofile)
            .setContentTitle("FCM Message")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

}

AndroidManifest.xml

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

<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".DashBoardActivity"
        android:label="@string/title_activity_dash_board"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>

    <activity
        android:name=".NewOrderActivity"
        android:label="@string/title_activity_dash_board"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>

    <activity
        android:name=".PostLoginActivity"
        android:label="@string/title_activity_dash_board"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>
</application>

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

【问题讨论】:

标签: android firebase google-cloud-messaging firebase-cloud-messaging


【解决方案1】:

“您还必须记住,要接收从 Firebase 控制台发送的消息,应用程序必须在后台,既不能启动也不隐藏。” --- 根据@Laurent Russier 的评论。

在我将应用置于后台之前,我从未收到任何来自 Firebase 的消息。

这仅适用于模拟器的 USB 连接,您也会在前台收到通知

【讨论】:

  • 我必须打开应用程序然后按下主页按钮?
  • 不,您需要确保您的应用处于后台才能从控制台获取通知消息
  • 您可以使用pushtry.com 进行测试,比 Firebase 控制台更容易。您还可以发送自定义 JSON。
  • 感谢@HtmlTosin!在我从 GCM 迁移到 FCM 的应用程序上,它也对我有用。但是,当我在以下示例项目中进行测试时,即使应用程序在前台,我也可以通过 Firebase 控制台发送推送通知:github.com/ton1n8o/FCMTutorial Weird...
  • 我没有进入前台,即使在手机上安装了应用程序构建之后
【解决方案2】:

您已将服务置于应用程序标签之外。把底部改成这个。

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

</application>

【讨论】:

  • 不知道为什么很多教程都没有列出细节
  • 可能是因为现在不需要了
  • @tristan 我不知道为什么我有同样的问题,我的服务仍然像你提到的那样在应用程序中。我认为我的设备 ID 存储在 Firebase 服务器中
【解决方案3】:

我遇到了同样的问题,通过在我的服务中定义启用、导出为 true 解决了这个问题

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

【讨论】:

  • 这对我有用。实际上,我发现我只在我的应用程序运行时才收到通知。如果我重新启动手机,通知将不会通过。但是在设置enabledexported 属性之后,推送通知现在可以正常发送了。谢谢,霍比! :-)
  • 我认为将相同的两个属性也添加到您的 MyInstanceIDListenerService 服务中可能是个好主意。
  • 谢谢。我尝试了很多东西,但这是解决方案。你知道为什么会这样吗?
  • 没有必要。尝试过 Firebase 示例消息传递项目 - 有和没有“启用”和“导出” - 这是过度的。帮助运行和隐藏应用程序以在顶部栏查看通知 - 请参阅 Html Tosin 的答案。
  • 根据 android 文档,默认启用为 true,exported 表示服务是否可以由应用程序外部的组件调用。不知道为什么这里有必要这样做。 developer.android.com/guide/topics/manifest/…
【解决方案4】:

在 Overriden 方法中调用 super.OnMessageReceived()。这对我有用! 终于!

【讨论】:

    【解决方案5】:

    我遇到了设备未收到 Firebase 云消息的相同问题。

    在我的情况下,在 Firebase 控制台项目上定义的包名称与在我的 Android 项目的 Manifest 和 Gradle 上定义的包名称不同。

    结果我正确收到了令牌,但根本没有消息。

    总而言之,Firebase 控制台包名称必须与 Manifest & Gradle 匹配。

    您还必须记住,要接收从 Firebase 控制台发送的消息,应用程序必须在后台,既不启动也不隐藏。

    【讨论】:

    • "您还必须记住,要接收从 Firebase 控制台发送的消息,应用程序必须在后台,既没有启动也没有隐藏。"
    • 你说“没有开始也没有隐藏”是什么意思?
    【解决方案6】:

    我遇到了同样的问题,但我的清单中仍然有一些旧 GCM 的碎片。当我从清单中获得以下许可时,它修复了错误。 com.google.android.c2dm.permission.RECEIVE

    【讨论】:

      【解决方案7】:

      我遇到了类似的问题,但在我的情况下,我的 Gradle 构建中缺少 google-services plugin(我正在将 Firebase 添加到现有项目中)。

      我将以下内容添加到我的根build.gradle

      classpath 'com.google.gms:google-services:3.1.0'
      

      以下到我的app-level build.gradle结尾:

      apply plugin: 'com.google.gms.google-services'
      

      然后我必须从 Firebase 控制台下载 google-services.json 文件(最初导入了现有的 Google Cloud 项目)并将其复制到我的 app 目录`。

      【讨论】:

      • 我记得在 firebase 配置步骤中阅读此内容,但从未了解这些行的确切位置。 :(
      【解决方案8】:

      如果您刚刚将 FCM 添加到现有应用程序,onTokenRefresh() 将不会被调用。我通过卸载应用程序并重新安装它来运行它。

      【讨论】:

        【解决方案9】:

        就我而言,我注意到 mergemanifest 缺少接收器。所以我必须包括:

         <receiver
                    android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
                    android:exported="true"
                    android:permission="com.google.android.c2dm.permission.SEND" >
                    <intent-filter>
                        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                    </intent-filter>
         </receiver>
        

        【讨论】:

        • 即使我使用的是最新的 firebase 18.0.0,我仍然没有收到通知。但你的解决方案对我有用。
        • 我认为 FirebaseInstanceIdService 已经贬值了。这是什么接收器
        【解决方案10】:

        在这个问题上花了几个小时后,当我故意破坏“google-services.json”的格式并且我的构建仍然成功时,我终于意识到了这个问题。我的 IDE(AndroidStudio) 没有注意到此文件中的更改。我尝试了很多东西,“从磁盘重新加载”、gradle 任务“cleanBuildCache”、新的虚拟设备、卸载/重新安装应用程序等,但都没有成功。

        对我来说,解决方案在 AndroidStudio -&gt; Build -&gt; Clean ProjectBuild -&gt; Rebuild Project

        PS:还要确保“google-services.json”在“app”文件夹中。

        【讨论】:

        • 感谢您发布此信息。我的应用程序在 FCM 控制台中的包名与我本地的包名不同。即使我在 google-services.json 中修复了这个问题,我仍然必须卸载应用程序、清理构建、重建、重新部署。谢谢!
        【解决方案11】:

        在我的例子中,我只是在模拟器中打开了 WiFi 和移动数据,它就像一个魅力。

        【讨论】:

        • 在我的情况下它也是完美的,但问题是为什么会发生这种情况?为什么我们需要关闭/开启移动数据?
        • 我认为这都是在模拟器上模拟环境。就是这样。
        • 不仅在模拟器上,有时也会在真机上发生。
        【解决方案12】:

        您需要遵循以下清单代码(清单中必须有服务)

         <?xml version="1.0" encoding="utf-8"?>
            <manifest xmlns:android="http://schemas.android.com/apk/res/android"
                package="soapusingretrofitvolley.firebase">
                <uses-permission android:name="android.permission.INTERNET"/>
                <application
                    android:allowBackup="true"
                    android:icon="@mipmap/ic_launcher"
                    android:label="@string/app_name"
                    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=".MyFirebaseMessagingService">
                        <intent-filter>
                            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
                        </intent-filter>
                    </service>
        
                    <service
                        android:name=".MyFirebaseInstanceIDService">
                        <intent-filter>
                            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
                        </intent-filter>
                    </service>
                </application>
        
            </manifest>
        

        【讨论】:

        • 错误:已解决类“MyFirebaseInstanceIDService”
        【解决方案13】:

        当您复制您的设备令牌时,它可能会复制空间,请仔细检查您是否复制了正确的令牌

        【讨论】:

          【解决方案14】:

          我一直在阅读整篇文章和其他文章以及教程视频,但无法解决我没有收到消息的问题,但是注册令牌有效。

          在那之前,我只是在模拟器上测试应用程序。在实体手机上试用后,它立即运行,无需事先对项目进行任何更改。

          【讨论】:

            【解决方案15】:

            我遇到了这个问题,我检查了我的代码。我试图修复这里提到的所有内容。最后这个问题太愚蠢了。我设备的Sync 已关闭。这是我没有按预期收到通知的唯一原因。

            【讨论】:

            • 这很奇怪。我也禁用了同步,甚至没有在谷歌中授权。在一切正常之前,我几乎没有改变任何东西,但现在它没有连接。将进一步搜索。
            【解决方案16】:

            如果您希望您的应用也可以在 > 24 个版本中运行,请遵循以下步骤:

            1.在你的strings.xml中添加这个

            fcm_default_channel

            1. 在清单文件中添加此元数据:

            3.用于创建通知(带有图像)在您处理通知的地方使用此方法,如果直接添加到 firebasemessaging 服务,或者如果您正在使用某个 util 类,则添加该 util 类:

               @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                public static void createBigNotification(Bitmap bitmap, int icon, String message, Uri alarmSound) {
                    final int NOTIFY_ID = 0; // ID of notification
                    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
                    String id = mContext.getString(R.string.default_notification_channel_id); // default_channel_id
                    String title = mContext.getString(R.string.app_name);
                    Intent intent;
                    NotificationCompat.Builder builder;
                    if (notificationManager == null) {
                        notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
                    }
                    PendingIntent resultPendingIntent;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        int importance = NotificationManager.IMPORTANCE_HIGH;
                        NotificationChannel mChannel = notificationManager.getNotificationChannel(id);
                        if (mChannel == null) {
                            mChannel = new NotificationChannel(id, title, importance);
                            mChannel.enableVibration(true);
                            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                            notificationManager.createNotificationChannel(mChannel);
                        }
                        builder = new NotificationCompat.Builder(mContext, id);
                        intent = new Intent(mContext, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        resultPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
                        NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
                        bigPictureStyle.setBigContentTitle(title);
                        bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
                        bigPictureStyle.bigPicture(bitmap);
            
                        builder.setContentTitle(title)        // required
                                .setSmallIcon(R.drawable.app_icon)   // required
                                .setContentText(message) // required
                                .setDefaults(Notification.DEFAULT_ALL)
                                .setAutoCancel(true)
                                .setSound(alarmSound)
                                .setStyle(bigPictureStyle)
                                .setContentIntent(resultPendingIntent)
                                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
                                .setTicker(title)
                                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                    } else {
                        builder = new NotificationCompat.Builder(mContext, id);
                        intent = new Intent(mContext, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        resultPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
                        NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
                        bigPictureStyle.setBigContentTitle(title);
                        bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
                        bigPictureStyle.bigPicture(bitmap);
            
                        builder.setContentTitle(title)     // required
                                .setSmallIcon(R.drawable.app_icon)   // required
                                .setContentText(message) // required
                                .setDefaults(Notification.DEFAULT_ALL)
                                .setAutoCancel(true)
                                .setSound(alarmSound)
                                .setStyle(bigPictureStyle)
                                .setContentIntent(resultPendingIntent)
                                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
                                .setTicker(title)
                                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                                .setPriority(Notification.PRIORITY_HIGH);
                    }
                    Notification notification = builder.build();
                    notificationManager.notify(NOTIFY_ID, notification);
                }
            

            按照这些步骤操作,您的通知将出现在通知托盘中。

            【讨论】:

            • createBigNotification 方法是从哪里放的,又是从哪里调用的?
            • @Ssenyonjo 您可以将其添加到您的广播接收器或您将在应用程序中全局访问的活动/片段文件中。
            【解决方案17】:

            我按照firebase网站上的所有步骤设置了Android APP的发送通知。我尝试了很多次,但无论是在美德设备还是物理设备上,我都无法收到通知。在我更改了美德设备和物理设备中的一些设置(设置部分中关于接收通知的那些设置)后,虚拟设备和物理设备都开始接收通知。

            【讨论】:

              【解决方案18】:
              private void sendNotification(String message) {
                  Intent intent = new Intent(this, MainActivity.class);
                  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  int requestCode = 0;
                  PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
                  Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.URI_COLUMN_INDEX);
                  NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
                          .setSmallIcon(R.mipmap.ic_launcher)
                          .setContentText(message)
                          .setColor(getResources().getColor(R.color.colorPrimaryDark))
                          .setSmallIcon(R.mipmap.ic_launcher)
                          .setContentTitle("FCM Message")
                          .setContentText("hello").setLargeIcon(((BitmapDrawable) getResources().getDrawable(R.drawable.dog)).getBitmap())
                          .setStyle(new NotificationCompat.BigPictureStyle()
                                  .bigPicture(((BitmapDrawable) getResources().getDrawable(R.drawable.dog)).getBitmap()))
                          .setAutoCancel(true)
                          .setSound(sound)
                          .setContentIntent(pendingIntent);
              
                  NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                  notificationManager.notify(0, noBuilder.build());
              

              【讨论】:

              • 当我清楚我最近没有收到通知时,请帮助我如何解决这个问题,当我断开 USB 调试模式时,我的手机通知上下文不显示
              【解决方案19】:
                  <activity android:name=".activity.MainActivity">
                      <intent-filter>
                          <action android:name="android.intent.action.MAIN" />
              
                          <category android:name="android.intent.category.LAUNCHER" />
                      </intent-filter>
                  </activity>
                  <!-- Firebase Notifications -->
                  <service android:name=".service.MyFirebaseMessagingService">
                      <intent-filter>
                          <action android:name="com.google.firebase.MESSAGING_EVENT" />
                      </intent-filter>
                  </service>
              
                  <service android:name=".service.MyFirebaseInstanceIDService">
                      <intent-filter>
                          <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
                      </intent-filter>
                  </service>
              

              【讨论】:

              • 错误:已解决类“MyFirebaseInstanceIDService”
              • 您需要为 Firebase 服务创建一个 MyFirebaseInstanceIDService 类
              【解决方案20】:

              就我而言,我手动杀死了一些 google play 服务并忘记了它。

              稍后,我必须重新启动设备,然后我才能收到通知,没有任何问题。

              【讨论】:

                【解决方案21】:

                也许是来自连接 我将连接从以太网更改为无线,它没有做任何其他事情就可以工作

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2017-12-24
                  • 1970-01-01
                  • 2018-03-13
                  • 2021-11-20
                  • 2017-10-04
                  • 2020-07-16
                  • 1970-01-01
                  • 2020-06-16
                  相关资源
                  最近更新 更多