设备未收到Firebase云消息传递通知

xec*_*c86 55 android firebase google-cloud-messaging firebase-cloud-messaging

我有火力点云信息的问题,我从设备获取的令牌,并不过,在透过谷歌火力地堡通知控制台发送通知测试,该通知从未登录,也被推到了Android的虚拟设备.对于FCM的文档几乎一模一样,我有以下及一些其它在还有什么你必须做的就是推送通知与火力的工作方式的代码.我已经完成了所有的设置信息了(的build.gradle增加,安装谷歌播放服务等)的文件中规定,但仍然没有消息产生.我按下通知后立即收到一次性错误,指出"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());
}

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Tri*_*ard 51

您已将服务放在应用程序标记之外.改变底部.

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

</application>
Run Code Online (Sandbox Code Playgroud)

  • 我不知道为什么许多教程中没有列出 &lt;intent-filter&gt; 详细信息 (2认同)

Hob*_*oby 37

我有同样的问题,它通过在我的服务中定义enabled,导出为true来解决

  <service
            android:name=".MyFirebaseMessagingService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
Run Code Online (Sandbox Code Playgroud)

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

Htm*_*sin 33

"您还必须记住,要接收从Firebase控制台发送的消息,应用程序必须处于后台,而不是既未隐藏也未启动." ---根据@Laurent Russier的评论.

我从来没有从Firebase收到任何消息,直到我将我的应用程序放在后台.

这仅适用于模拟器的USB连接,您也可以在前台获得通知


小智 10

调用super.OnMessageReceived()Overriden方法.这对我有用!最后!


Jua*_*blo 7

我遇到了设备未收到的Firebase云消息传递问题.

在我的案例中,Firebase控制台项目中定义的包名称与我在Android项目的Manifest&Gradle上定义的包名称不同.

结果我正确地收到了令牌,但根本没有收到任何消息.

要进行sumarize,Firebase控制台包名称和Manifest&Gradle必须匹配.

您还必须记住,要接收从Firebase控制台发送的消息,应用程序必须位于后台,不能同时启动.

  • "您还必须记住,要接收从Firebase控制台发送的消息,应用程序必须处于后台,而不是既未隐藏也未启动." (7认同)
  • 当你说“不开始也不隐藏”时,你是什么意思? (2认同)

tx8*_*802 7

我遇到了类似的问题,但在我的情况下,我错过了Gradle版本中的google-services插件(我将Firebase添加到现有项目中).

我在我的根目录中添加了以下内容build.gradle:

classpath 'com.google.gms:google-services:3.1.0'
Run Code Online (Sandbox Code Playgroud)

和以下到我的app级别的结尾build.gradle:

apply plugin: 'com.google.gms.google-services'
Run Code Online (Sandbox Code Playgroud)

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


RSS*_*SSV 7

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


Tas*_*iwa 6

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


Can*_*ner 6

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

对我来说,解决方案AndroidStudio -> Build -> Clean ProjectBuild -> Rebuild Project

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


Irs*_*shu 5

就我而言,我注意到 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>
Run Code Online (Sandbox Code Playgroud)


vah*_*bet 5

就我而言,我只需在模拟器中打开 WiFi 和移动数据,它就非常有用。