I manage to set up everything and I can sent notification from method 1(Firebase Console) or 2(HTTP Send using Javascript and Ajax)
Now after message sent, when user click on the notification, how to know it is from method 1(Firebase Console) or 2(HTTP Send using javascript and Ajax)?
The reason is I wish to redirect user to different page on the app.
Can I use the title? Method 2 message will always have a static title, if so how do I do it, please help.
Firebase Console.
HTTP send
function post() {
$.ajax({
type : 'POST',
url : "https://fcm.googleapis.com/fcm/send",
headers : {
Authorization : 'key=' + '<%=sKey%>'
},
contentType : 'application/json',
dataType: 'json',
data: JSON.stringify({
"to": "<%=sToWhichDevice%>",
"priority": "high",
"notification": {
"sound": "defaultSound",
"title":"<%=sTitle%>",
"body":"<%=sBody%>"
}
}),
success : function(response) {
console.log(response);
},
error : function(xhr, status, error) {
console.log(xhr.error);
}
});
}
window.onload = post();
Detect when user click on the notification
//=== Handle notification messages after display notification is tapped by the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response
NSDictionary *userInfo = response.notification.request.content.userInfo;
//==== HOW DO I DIFFERENTIATE IT IS SEND from FireBase Console or HTTP Javascript Ajax send?
if (userInfo[kGCMMessageIDKey]) {
NSLog(@" **5 : Notification from Firebase Console. Message ID: %@",
}else{
NSLog(@" **5** : Notification from HTTP Ajax");
}
completionHandler();
}
For posterity, I will transform my comment into a full answer.
FCM push notifications payload have several predefined keys, required by iOS so that the system can automatically display the notification on a device (eg. title, message, icon, etc). You have to remember that as an iOS developer you don't have too many options when it comes to create a custom push notification UI (but that's another topic).
On the other hand, FCM (and almost all other push notification services) provide a way to send custom data via push notification. For FCM you can use the data
key to define a list of key-value pairs that will be delivered within the push notification.
Now implementation wise, it's your choice what you send there, as I suggested you can put something like:
"data":{
"sent-from" : "MyAwesomeSite",
"why" : "Because I have to!",
"when" : "ASAP"
}
Now it's up to you what you add as key-value pair, but keep in mind that the size of the payload is limited and there is a OS limitation and I also saw limitation enforced by the push notification service providers.