Firebase 控制台消息未显示在模拟器或设备上



我在Visual Studio 2017中编码。在安卓应用程序上工作,试图让推送通知正常工作。我在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 中的 OnCreate 片段.cs

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;

这都是关于AndroidManifest文件。一切都需要在应用程序标签中,我需要添加一个接收器消息传递服务。您可以在下面看到新的 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>

相关内容

  • 没有找到相关文章

最新更新