亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 xamarin 上正確接收 Intent Extras

如何在 xamarin 上正確接收 Intent Extras

C#
倚天杖 2022-12-31 12:54:14
目前,我正在將本教程用于 xamarin 推送通知。它就像一個魅力。我現在的目標是正確處理意圖。在我的 firebase 消息傳遞服務中,我創建了一個通知并添加了附加信息。private void CreateNotification(Object e){    try    {        string title = "";        string body = "";        var intent = new Intent(this, typeof(MainActivity));        var i = e as Intent;        var bundle = i.Extras;        title = bundle.GetString("gcm.notification.title");        body = bundle.GetString("gcm.notification.body");        intent.PutExtra("view", "test");        intent.PutExtra("title", title);        intent.PutExtra("body", body);        intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.CancelCurrent | PendingIntentFlags.UpdateCurrent);        Notification.Builder builder = new Notification.Builder(this);        builder.SetSmallIcon(Resource.Drawable.icon_notification);        builder.SetContentIntent(pendingIntent);        builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.icon_notification));        builder.SetContentTitle("Test");        builder.SetContentText(body);        builder.SetDefaults(NotificationDefaults.Sound);        builder.SetAutoCancel(true);        NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);        notificationManager.Notify(1, builder.Build());    }    catch (Exception ex)    {        Console.WriteLine(ex.Message);    }}目前后臺推送通知處理得很好。我能夠在其中的OnCreate方法中運行一條簡單的線MainActivity.csIntent.GetStringExtra("view");這讓我得到了在 CreateNotification 中設置的值。我遇到的問題是試圖將意圖放在前臺。此代碼駐留在 MainActivity.cs 上,當在前臺單擊推送通知時觸發。protected async override void OnNewIntent(Intent intent){    base.OnNewIntent(intent);    if (Intent.Extras != null)    {        foreach (var key in Intent.Extras.KeySet())        {            var value = Intent.Extras.GetString(key);            Log.Debug(TAG, "Key: {0} Value: {1}", key, value);        }    } }Intent.Extras 始終為空,請問我哪里出錯了。
查看完整描述

3 回答

?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

也許答案遲了,但它可以幫助其他人。

唯一的錯誤是您使用Intent代替intent(傳遞的參數)

if (Intent.Extras != null)

代替

if (intent.Extras != null)

我陷入了同樣的分心。


查看完整回答
反對 回復 2022-12-31
?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

請通過這個。 實施 FirebaseMessagingService - 前臺通知

您必須創建 FirebaseMessagingService,當您的應用程序處于前臺時,您可以在其中接收 RemoteMessage。


查看完整回答
反對 回復 2022-12-31
?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

實際上,根據文檔,處理前臺通知的正確方法是實現一個FirebaseMessagingService


這里有更好的解釋


您需要創建一個類似這樣的服務類:


    [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.Drawable.ic_stat_ic_notification)

                .SetContentTitle("Title")

                .SetContentText(messageBody)

                .SetAutoCancel(true)

                .SetContentIntent(pendingIntent);


            NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);

            notificationManager.Notify(0, notificationBuilder.Build());

        }

        catch (Exception ex)

        {


        }

       }

     }

有關更多信息,您可以查看我的 SO 問題


如何處理 Firebase 通知,即 Android 中的通知消息和數據消息


查看完整回答
反對 回復 2022-12-31
  • 3 回答
  • 0 關注
  • 169 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號