Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Action in notifications #325

Closed
FlaviooLima opened this issue Feb 21, 2017 · 14 comments · Fixed by #773
Closed

[Question] Action in notifications #325

FlaviooLima opened this issue Feb 21, 2017 · 14 comments · Fixed by #773

Comments

@FlaviooLima
Copy link

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

@evollu
Copy link
Owner

evollu commented Feb 24, 2017

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

@FlaviooLima
Copy link
Author

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

@alialamine
Copy link
Contributor

alialamine commented Feb 24, 2017

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

@evollu
Copy link
Owner

evollu commented Feb 24, 2017

this is awesome

@samcomashish
Copy link

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.

@mcmatan
Copy link

mcmatan commented Apr 27, 2017

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

@alialamine
Copy link
Contributor

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

@mcmatan
Copy link

mcmatan commented Apr 27, 2017

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?

@alialamine
Copy link
Contributor

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)

@mcmatan
Copy link

mcmatan commented Apr 28, 2017

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
        }

@LMestre14
Copy link

LMestre14 commented Jul 11, 2017

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

@xuanloi1408
Copy link

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",

@DanielRamosAcosta
Copy link

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

@evollu
Copy link
Owner

evollu commented Dec 5, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants