Firebase push notification adding action buttons
FCM doesn't support this functionality right now. The solution would be to send a data message via FCM and create your own notification.
- Send data message with your own key-value pairs:
"data": {
"key": "value",
"key2": "value2"
}
- Receive messages, use a service that extends
FirebaseMessagingService
. - Override the
onMessageReceived
callback to create notification based on your data message.
https://firebase.google.com/docs/cloud-messaging/android/receive
How to handle push notification Action taps?
So I was able to figure out what (I think) I needed by following some of what Barel answered as well as some other SO pages and piecing this together.
I'm sure I'll run into some changes I need to make later but, for now, this got me to where I could have 2 actions (Accept, Decline), select either of the action buttons, and then hit some logic where I'm going to implement what I want to do with both actions.
SendLocalNotification method in FirebaseService class:
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());
}
And then I had to update my OnNewIntent override in MainActivity. Notice the manager.CancelAll()
. I had to do this to actually remove the notification from my bar after clicking the action:
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);
}
Is there any way to implement push notifications with action buttons in flutter? Using firebase_messaging
The firebase_messaging package does not provide any solution for action buttons. You can get your data
in your onMessageReceived()
method. And you can create your own customized notification.
How to dismiss notification after action button clicked without opening the app?
Found the answer while surfing the GitHub Repository of awesome_notifications.
The notification
can straightly be dismissed without opening the app by adding buttonType
as ActionButtonType.DisabledAction
in the NotificationActionButton
just like this:
NotificationActionButton(
key: 'DISMISS',
label: 'Dismiss',
autoDismissible: true,
buttonType: ActionButtonType.DisabledAction,
isDangerousOption: true
)
Note: Doing so will not trigger any receivedAction
in the actionStream
.
Notification not displayed with action buttons when app is killed ios
There is two way to achieve this action handling in Push Notification.
1) Is simple register your UNUserNotificationCenter which you are currently using but you are facing issues with action button not showing in killed mode.
So please check your payload user info dictionary identifier & category with your configuration which you shared.
Following is my code please check this Swift code.
private func setNotificationCategories() {
let likeAction = UNNotificationAction(identifier: "like", title: "Like", options: [])
let replyAction = UNNotificationAction(identifier: "reply", title: "Reply", options: [])
let archiveAction = UNNotificationAction(identifier: "archive", title: "Archive", options: [])
let ccommentAction = UNTextInputNotificationAction(identifier: "comment", title: "Comment", options: [])
let localCat = UNNotificationCategory(identifier: "category", actions: [likeAction], intentIdentifiers: [], options: [])
let customCat = UNNotificationCategory(identifier: "recipe", actions: [likeAction,ccommentAction], intentIdentifiers: [], options: [])
let emailCat = UNNotificationCategory(identifier: "email", actions: [replyAction, archiveAction], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([localCat,
customCat, emailCat])
}
And I Updated your code please check this
UNNotificationCategory *modifyCategory = [UNNotificationCategory categoryWithIdentifier:CYLInviteCategoryIdentifier actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
UNNotificationAction* snoozeAction = [UNNotificationAction
actionWithIdentifier:@"ACCEPT_ACTION"
title:@"Accept"
options:[]];
UNNotificationAction* stopAction = [UNNotificationAction
actionWithIdentifier:@"DECLINE_ACTION"
title:@"Decline"
options:[]];
UNNotificationCategory* actionCategory = [UNNotificationCategory
categoryWithIdentifier:@"INCOMING_CALL"
actions:@[snoozeAction, stopAction]
intentIdentifiers:@[]
options:[]];
[center setNotificationCategories:[NSSet setWithObjects: modifyCategory, actionCategory,
nil]];
change is just don't set option.
2)Use Notification extension You can design attractive push notification. for that, I suggest prefered this blog iOS Remote Push Notifications
Open App from Push-Notification's Action Buttons in iOS 11+ (Swift)?
If you are creating the actions UNNotificationAction(identifier: ,
title:,
options: UNNotificationActionOptions.foreground)
select UNNotificationActionOptions.foreground for the options parameter and it will lunch the app when the actions clicked. For more information, have a look at the Apple documentation for foreground and UNNotificationActionOptions
To add some task after lunching the app, use userNotificationCenter(_:didReceive:withCompletionHandler:) delegate method in the app delegate.
Related Topics
Uitextfield Clearbuttonmode Color
Avcapturevideopreviewlayer Landscape Orientation
iPhone - Didselectrowatindexpath: Only Being Called After Long Press on Custom Cell
I am Getting Unsupported Parameter Combination Cgbitmap Error with Swift
How to Disable Arc for a Single File in Xcode 5
Could Not Attach to Pid:"####" Unable to Attach
What Is Container View in iOS 5 Sdk
How to Disable Custom Keyboards (Ios8) for My App
Dynamic Uicollectionview Header Size Based on Uilabel
iOS Enterprise Provisioning Profile Expiration
Uidatepicker Bug? Uicontroleventvaluechanged After Hitting Minimum Internal
How to Get Audio Controls on Lock Screen/Control Center from Avaudioplayer in Swift
Add Uiview Banner Above Status Bar iOS 7
How to Get Previous and Next Month in Calendar-Swift
Get Uiwebview Content's Height
While Mail Sending Process Using iOS 8 Simulators Generates Error