Firebase控制台消息未在仿真器或设备上显示

时间:2018-06-08 14:45:12

标签: android firebase firebase-cloud-messaging

我在Visual Studio 2017中进行编码。在Android应用程序上工作,试图让推送通知正常工作。我在Firebase上创建了一个应用,Firebase看到了该设备。当我通过Firebase控制台发送消息时,使用在触发OnTokenRefresh事件时生成的令牌,即使Firebase表示已完成,设备也不会收到消息。我在c#编码。我按照这个例子

https://blog.xamarin.com/implementing-push-notifications-android-apps/

MyFirebaseIIDService.cs

using Android.App;
using Firebase.Iid;
using Android.Util;

namespace SIS.Apps.Droid
{
[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class MyFirebaseIIDService : FirebaseInstanceIdService
{
    const string TAG = "MyFirebaseIIDService";

    public override void OnTokenRefresh()
    {
        var refreshedToken = FirebaseInstanceId.Instance.Token;
        Log.Debug(TAG, "Refreshed token: " + refreshedToken);
        SendRegistrationToServer(refreshedToken);
    }
    void SendRegistrationToServer(string token)
    {
       //bool Success = Globals.CLIENT.RegisterDevice(token);
    }
   }
}

MYFirebaseMessagingService.cs

using System;
using Android.App;
using Android.Content;
using Firebase.Messaging;
using Android.Support.V4.App;
namespace SIS.Apps.Droid
{
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
    // private string TAG = "MyFirebaseMsgService";
    public override void OnMessageReceived(RemoteMessage message)
    {
        base.OnMessageReceived(message);
        string messageFrom = message.From;
        string getMessageBody = message.GetNotification().Body;
        SendNotification(message.GetNotification().Body);
    }
    void SendNotification(string messageBody)
    {
        try
        {
            var intent = new Intent(this, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .SetSmallIcon(Resource.Mipmap.icon)
                .SetContentTitle("Title")
                .SetContentText(messageBody)
                .SetAutoCancel(true)
                .SetContentIntent(pendingIntent);

            NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
            notificationManager.Notify(0, notificationBuilder.Build());
        }
        catch (Exception ex)
        {

        }
    }
}
} 

MainActivity.cs中的OnCreate片段

FirebaseApp.InitializeApp(Android.App.Application.Context);            

        System.Threading.Tasks.Task.Run(async () =>
        {
            var instanceID = FirebaseInstanceId.Instance;
            instanceID.GetToken("FIREBASE_AUTH", FirebaseMessaging.InstanceIdScope);
        });
        var token = FirebaseInstanceId.Instance.Token;

1 个答案:

答案 0 :(得分:0)

这完全是关于AndroidManifest文件的。一切都需要在Application标签内,我需要在MessagingService中添加一个接收器。您可以在下面看到新的xml文件。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="SIS.Apps.Android" android:installLocation="auto">
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<application android:label="SIS.Apps.Android">
    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
    <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" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="${applicationId}" />
        </intent-filter>
    </receiver>
    <receiver android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </receiver>
</application>
</manifest>