gpt4 book ai didi

c# - FCM(FirebaseMessagingService)OnNewToken 没有被 Android 击中

转载 作者:行者123 更新时间:2023-12-04 11:55:34 29 4
gpt4 key购买 nike

我按照 this page 上的说明进行操作创建推送通知。我之前实际上已经做过一次并且能够让它工作(几周前),花了一些时间,我想我现在才再次做这个教程作为复习,出于某种原因,我可以'甚至获取代码以点击 OnNewToken 方法来生成我的 token 并将设备注册到通知中心。

我看过几十个视频,阅读其他教程,他们都在说/展示几乎相同的东西,所以我想我需要一双新的眼睛来告诉我我第二次错过了什么。

我试图提取特定信息,但仍尽可能保持其可读性。

已安装的 NuGet 包:

  • Xamarin.Firebase.Messaging - v71.1740.0
  • Xamarin.GooglePlayServices.Base - v71.1610.0
  • Xamarin.Forms - v4.4.0.991640

  • Android 项目中的文件
    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="(my firebase package / project package name)" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET" />
    <!--
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    -->
    <application>
    <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>
    </application>
    </manifest>

    应用常量
    public static class AppConstants
    {
    public static string NotificationChannelName { get; set; } = "XamarinNotifyChannel";
    public static string NotificationHubName { get; set; } = "(my azure notification hub name)";
    public static string ListenConnectionString { get; set; } = "(my default listen shared access signature from azure portal)";
    public static string DebugTag { get; set; } = "XamarinNotify";
    public static string[] SubscriptionTags { get; set; } = { "default" };
    public static string FCMTemplateBody { get; set; } = "{\"data\":{\"message\":\"$(messageParam)\"}}";
    public static string APNTemplateBody { get; set; } = "{\"aps\":{\"alert\":\"$(messageParam)\"}}";
    }

    Firebase 服务
    [Service(Name = "(my package name).MyFirebaseMessagingService")]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class FirebaseService : FirebaseMessagingService
    {
    public override void OnNewToken(string token)
    {
    base.OnNewToken(token);
    Console.WriteLine("NEW_TOKEN", token);
    SendRegistrationToServer(token);
    }

    void SendRegistrationToServer(string token)
    {
    NotificationHub hub = new NotificationHub(AppConstants.NotificationHubName, AppConstants.ListenConnectionString, this);
    // register device with Azure Notification Hub using the token from FCM
    Registration reg = hub.Register(token, AppConstants.SubscriptionTags);
    // subscribe to the SubscriptionTags list with a simple template.
    string pnsHandle = reg.PNSHandle;
    hub.RegisterTemplate(pnsHandle, "defaultTemplate", AppConstants.FCMTemplateBody, AppConstants.SubscriptionTags);
    }

    public override void OnMessageReceived(RemoteMessage message)
    {
    base.OnMessageReceived(message);
    string messageBody = string.Empty;
    if (message.GetNotification() != null)
    {
    messageBody = message.GetNotification().Body;
    }
    else
    {
    messageBody = message.Data.Values.First();
    }
    try
    {
    MessagingCenter.Send(messageBody, "Update");
    }
    catch (Exception e)
    { }
    SendLocalNotification(messageBody);
    }

    void SendLocalNotification(string body)
    {
    var intent = new Intent(this, typeof(MainActivity));
    intent.AddFlags(ActivityFlags.ClearTop);
    intent.PutExtra("message", body);

    //Unique request code to avoid PendingIntent collision.
    var requestCode = new Random().Next();
    var pendingIntent = PendingIntent.GetActivity(this, requestCode, intent, PendingIntentFlags.OneShot);

    var notificationBuilder = new NotificationCompat.Builder(this)
    .SetContentTitle("XamarinNotify Message")
    .SetSmallIcon(Resource.Drawable.ic_launcher)
    .SetContentText(body)
    .SetAutoCancel(true)
    .SetShowWhen(false)
    .SetContentIntent(pendingIntent);

    if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
    {
    notificationBuilder.SetChannelId(AppConstants.NotificationChannelName);
    }

    var notificationManager = NotificationManager.FromContext(this);
    notificationManager.Notify(0, notificationBuilder.Build());
    }
    }

    谷歌服务.json
    我刚刚从 Firebase 下载了这个文件,将它添加到 Android 项目并设置了 构建操作 到 GoogleServicesJson。

    希望有人能看到我缺少的东西,因为我之前也有过同样的教程。

    最佳答案

    请在您的 android 中卸载该应用程序,然后重新部署它。
    onNewToken()每次安装只会调用一次。

    如果您需要再次调用它,请从您的设备中卸载该应用程序并重新启动它。

    关于c# - FCM(FirebaseMessagingService)OnNewToken 没有被 Android 击中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60369954/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com