evollu / react-native-fcm

react native module for firebase cloud messaging and local notification

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] Action in notifications

FlaviooLima opened this issue · comments

It's possible to do this in local and/or push notifications?
To put Button that will trigger actions in the app if the app is open.
screen shot 2017-02-21 at 15 31 19

Yes, but not yet implemented.
This involves 2 parts:

  1. A standard way to build these buttons through configuration
  2. Send proper event back to JS thread

help is welcomed

I can't promise you that i can do much, but i will try :)
Can you explain me what you need from me :)

I didn't send a pull-request for all of this as I don't know how to make something that can also work on Android, so when I get to Android I will see if they are close enough to have a common code in js. For now this is my solution to have this feature:

Step 1:
This is the only step that has to be in native code. Add a description of your actions in the application: function of your app. To do this you need to create a NotificationCategory. This would be something similar to this:

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNNotificationAction *viewAction = [UNNotificationAction actionWithIdentifier:@"ViewJobRequest" title:@"View" options:UNNotificationActionOptionForeground];
UNNotificationAction *rejectAction = [UNNotificationAction actionWithIdentifier:@"RejectJobRequest" title:@"Reject" options:UNNotificationActionOptionDestructive];
UNNotificationCategory *actionsCategory = [UNNotificationCategory categoryWithIdentifier:@"JobActions" actions:@[viewAction, rejectAction] intentIdentifiers:@[] options:nil];
[center setNotificationCategories:[[NSSet alloc] initWithArray:@[actionsCategory]]];

Step 2:
When you send the notification, include the identifier of the actionCategory into the click_action index (for the actions above, that would be the following):

FCM.presentLocalNotification({
            ...
            click_action: 'JobActions',
            ...
        });

Step 3:
To detect which button is pressed, after #328 gets merged and published, you can find the identifier of the action ("ViewJobRequest", "RejectJobRequest") in the action_identifier of the notification response.

Edit:
We ended up optin for _actionIdentifier as a key in the notification response instead of action_identifier

this is awesome

Hi friends, I need this functionality asap, if you help me some doc or something I will surely create interactive notification. I have android structure idea not much in ios.

Thank you.

Followed all steps, but actions do not display when pressing notification...
Using version 6.2.0

@mcmatan can you please show us the code you put in the ApplicationDelegate and the part that sends the notification?

Hi @alialamine and thanks for your quick response.
I've added your exact blob of code at the end of application didFinishLaunchingWithOptions: in by AppDelegate.

This code:

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNNotificationAction *viewAction = [UNNotificationAction actionWithIdentifier:@"ViewJobRequest" title:@"View" options:UNNotificationActionOptionForeground];
UNNotificationAction *rejectAction = [UNNotificationAction actionWithIdentifier:@"RejectJobRequest" title:@"Reject" options:UNNotificationActionOptionDestructive];
UNNotificationCategory *actionsCategory = [UNNotificationCategory categoryWithIdentifier:@"JobActions" actions:@[viewAction, rejectAction] intentIdentifiers:@[] options:nil];
[center setNotificationCategories:[[NSSet alloc] initWithArray:@[actionsCategory]]];

And when sending push notification to

https://fcm.googleapis.com/fcm/send

Inside the notification payload, I've added:

        click_action: 'JobActions',

Exactly as your example, what am I missing?

I did not try it with remote notifications, so can you try sending the notification with the library's local notification? Just want to eliminate stuff to find where is the things we're not noticing.

And can you put an example of the full object that you are sending to fcm? I just want to try it to see the full picture (just replace the data with xxx for whatever you don't want to show)

So I've tried using local notification and it works, the problem is with sending remote notifications

Payload I'm sending:


**notification:**
badge : 1
body :"Press here"
click_action : "JobActions" 
id : "123"
priority : "high"
sound :"incomecallring.mp3"
title : "Your title"

"to": push_token,
        "content_available": contentAvailable,
        "notification": notification,
        "priority": "high",
        "data": {
            "notification": notification
        }

I was able to have action button on notification, with fcm

This is what I did:

File AppDelegate.m
instead of having
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]

I inserted

UNUserNotificationCenter *currentNotifCenter =[UNUserNotificationCenter currentNotificationCenter];
UNNotificationAction *clickAction = [UNNotificationAction actionWithIdentifier:@"Action" title:@"My Button Title" options:UNNotificationActionOptionForeground];
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"Category" actions:@[clickAction] intentIdentifiers:@[] options: UNNotificationCategoryOptionNone];
NSSet *categories = [NSSet setWithObject: category];
[currentNotifCenter setNotificationCategories: categories];
[currentNotifCenter setDelegate:self];

And for the payload I did

"registration_ids": [
            "token"
        ],
    "content_available": true,
    "notification": {
		"title": "Notification Title",
		"body": "Notification Message",
		"click_action": "Category"
	},
    "data": {
    }

The click action has to be exactly the same as the UNNotificationCategory identifier
On press of the button you will have access to all the info you pass on the payload, local or remote

if you want to verify if the user clicked on the button in FCM.on, you only have to verify if notif._actionIdentifier is equal to the identifier you added to you button

I using fcm notification but error on register:

[Firebase/Messaging][I-FCM012002] Error in application:didFailToRegisterForRemoteNotificationsWithError: no valid 'aps-environment' entitlement string found for application

Can You help me, please?

I using "react-native-fcm": "^7.1.0",

Is this possible to manage without touching native code? Just with this library?

@DanielRamosAcosta It is possible but I'm not planning to develop it yet. PRs welcome