设备未收到Firebase云消息通知 [英] Firebase cloud messaging notification not received by device

查看:137
本文介绍了设备未收到Firebase云消息通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了FireBase云消息传递问题,我从设备获取令牌,然后通过Google Firebase通知控制台发送通知测试,但通知从不记录,也不会推送到android虚拟设备。 FCM的文档几乎就是我下面的代码,还有一些其他的东西你还得做些什么才能使推送通知与Firebase一起工作。我已经通过了文档中指定的所有设置信息(build.gradle添加,安装google play服务等),但仍然没有生成消息。我推送通知后立即收到一次性错误,指出I / FA:Tag Manager未找到,因此不会被使用,但这是唯一的数据输出,我没有找到任何与标签经理要求和FCM上的谷歌。代码有什么问题,我没有收到我的推送通知到logcat或设备?请让我知道任何进一步的信息,这将是有益的。
谢谢。



NotificationGenie.java

  public class NotificationGenie extends FirebaseMessagingService {

private static final String TAG =Firebase_MSG;

@Override
public void onMessageReceived(RemoteMessage remoteMessage){
// TODO(developer):在这里处理FCM消息。
//如果应用程序在前台处理数据和通知消息。
//同样,如果您打算根据收到的FCM
//消息生成您自己的通知,那么这里是应该启动的地方。请参阅下面的sendNotification方法。
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 * /,notificationBuilder.build());




$ b

AndroidManifest.xml

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

<! - 要使用用户的电子邮件自动填写登录表单中的电子邮件文本字段 - >
< uses-permission android:name =android.permission.GET_ACCOUNTS/>
<使用权限android:name =android.permission.READ_PROFILE/>
<使用权限android:name =android.permission.READ_CONTACTS/>
< uses-permission android:name =android.permission.INTERNET/>

应用程序$ b $ 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>

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

android:name =。NewOrderActivity
android:label =@ string / title_activity_dash_board
android:theme =@ style / AppTheme .NoActionBar>
< / 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>


解决方案

您已将您的服务置于应用程序标记之外。

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

< / application>


I am having an issue with FireBase Cloud Messaging in which I get the Token from the device and send the notification test through the Google Firebase notification console however, the notification is never logged nor pushed to the android virtual device. The documentation for FCM is almost exactly the code that I have below and little else in the way of what else you would have to do to get push notifications working with firebase. I have gone through all of the setup information (build.gradle additions, Installing google play services, etc...) as specified in the documentation but still do not have messages generating. I do receive a one-time error immediately after pushing the notification that states "I/FA: Tag Manager is not found and thus will not be used" but that is the only data that is output and I did not find anything related to Tag Manager requirements and FCM on the googles. What is wrong with the code that I am not receiving my push notifications to the logcat or the device? Please let me know any further information that would be helpful. Thanks.

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>

解决方案

You have placed your service outside the application tag. Change bottom to this.

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

</application>

这篇关于设备未收到Firebase云消息通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆