【问题标题】:Firebase Console Messages not Showing on Emulator or DeviceFirebase 控制台消息未显示在模拟器或设备上
【发布时间】:2018-11-18 15:53:27
【问题描述】:

我正在使用 Visual Studio 2017 进行编码。正在开发一个试图让推送通知正常工作的 Android 应用程序。我在 Firebase 上创建了一个应用,Firebase 看到了该设备。当我使用触发 OnTokenRefresh 事件时生成的令牌通过 Firebase 控制台发送消息时,即使 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 的 sn-p

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;

【问题讨论】:

  • 嘿迈克。只是想澄清一下,OnMessageReceived 没有 被触发还是只是设备上没有出现通知?此外,您通过控制台发送的数据样本也会有所帮助。干杯!
  • AL,永远不会触发 onMessageReceived。

标签: android firebase firebase-cloud-messaging


【解决方案1】:

这都是关于 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>

【讨论】:

    猜你喜欢
    • 2017-09-29
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    相关资源
    最近更新 更多