簡體   English   中英

如何處理推送通知動作點擊?

[英]How to handle push notification Action taps?

我有一個帶有 2 個操作(接受、拒絕)的推送通知。 如何確定實際點擊了哪個操作,然后讓通知消失(就像用戶點擊了實際通知而不是操作按鈕一樣)? 看起來兩個動作都將 go 輕拍到我的OnNewIntent方法中,我想根據哪個被輕拍來做不同的動作。 除了不再顯示操作(按鈕)之外,我的通知也仍然顯示在通知欄中。 當我點擊它時,通知會完全消失。

這是我在FirebaseService中的SendLocalNotification方法(忽略評論。它們是給我的):

void SendLocalNotification(string body)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.SingleTop);
        intent.PutExtra("OpenPage", "SomePage");
        //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)
            .AddAction(0, "Accept", pendingIntent) // THIS ADDS A BUTTON TO THE NOTIFICATION
            .AddAction(0, "Decline", pendingIntent) // THIS ADDS A BUTTON TO THE NOTIFICATION
            .SetContentTitle("Load Match")
            .SetSmallIcon(Resource.Drawable.laundry_basket_icon_15875)
            .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());
    }

然后我在MainActivityOnNewIntent

protected override void OnNewIntent(Intent intent)
    {
        if (intent.HasExtra("OpenPage"))
        {
            MessagingCenter.Send(Xamarin.Forms.Application.Current, "Notification");
        }

        base.OnNewIntent(intent);
    }

編輯:我的OnNewIntent目前具有該邏輯,因為我正在使用 MessagingCenter 在視圖 model 上向訂閱者發送消息並進行視圖導航。 然后我發現了 Actions,這就是我最終在這里嘗試使用通知按鈕執行該邏輯的方式。

使用“yes”、“no”和“maybe”動作創建Nofification:

//Yes intent
Intent yesReceive = new Intent();  
yesReceive.setAction(YES_ACTION);
PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.calendar_v, "Yes", pendingIntentYes);

//Maybe intent
Intent maybeReceive = new Intent();  
maybeReceive.setAction(MAYBE_ACTION);
PendingIntent pendingIntentMaybe = PendingIntent.getBroadcast(this, 12345, maybeReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.calendar_question, "Partly", pendingIntentMaybe);

//No intent
Intent noReceive = new Intent();  
noReceive.setAction(NO_ACTION);
PendingIntent pendingIntentNo = PendingIntent.getBroadcast(this, 12345, noReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.calendar_x, "No", pendingIntentNo);

然后創建廣播接收器以在單擊操作時調用:

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if(YES_ACTION.equals(action)) {
        Log.v("shuffTest","Pressed YES");
    } else if(MAYBE_ACTION.equals(action)) {
        Log.v("shuffTest","Pressed NO");
    } else if(NO_ACTION.equals(action)) {
        Log.v("shuffTest","Pressed MAYBE");
    }
}

發送動作時注意適當。

因此,通過遵循 Barel 回答的一些內容以及其他一些 SO 頁面並將它們拼湊在一起,我能夠弄清楚(我認為)我需要什么。

我確定我稍后會遇到一些需要進行的更改,但是,就目前而言,這讓我可以進行 2 個操作(接受、拒絕),select 任何一個操作按鈕,然后點擊一些邏輯我將用這兩個動作實現我想要做的事情。


FirebaseService class 中的SendLocalNotification方法:

void SendLocalNotification(string body)
    {
        //Unique request code to avoid PendingIntent collision.
        var requestCode = new Random().Next();

        // accept
        var acceptIntent = new Intent(this, typeof(MainActivity));
        acceptIntent.SetAction("ACCEPT_ACTION");
        var pendingIntentAccept = PendingIntent.GetActivity(this, requestCode, acceptIntent, PendingIntentFlags.OneShot);

        // decline
        var declineIntent = new Intent(this, typeof(MainActivity));
        declineIntent.SetAction("DECLINE_ACTION");
        var pendingIntentDecline = PendingIntent.GetActivity(this, requestCode, declineIntent, PendingIntentFlags.OneShot);

        var notificationBuilder = new NotificationCompat.Builder(this)
            .AddAction(0, "Accept", pendingIntentAccept)
            .AddAction(0, "Decline", pendingIntentDecline)
            .SetContentTitle("Load Match")
            .SetSmallIcon(Resource.Drawable.laundry_basket_icon_15875)
            .SetContentText(body)
            .SetAutoCancel(true)
            .SetShowWhen(false)
            .SetContentIntent(pendingIntentAccept)
            .SetContentIntent(pendingIntentDecline);

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

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

然后我必須在MainActivity更新我的OnNewIntent覆蓋。 注意manager.CancelAll() 單擊操作后,我必須這樣做才能真正從我的欄中刪除通知:

protected override void OnNewIntent(Intent intent)
    {
        switch (intent.Action)
        {
            case "ACCEPT_ACTION":
                Console.WriteLine("hit accept action case.");
                break;
            case "DECLINE_ACTION":
                Console.WriteLine("hit decline action case.");
                break;
            default:
                Console.WriteLine("didn't hit either action.");
                break;
        }

        var manager = (NotificationManager)this.GetSystemService(Context.NotificationService);
        manager.CancelAll();
        base.OnNewIntent(intent);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM