I m using GCM for iOS and I receive the message that I push but the message look like this object :
[aps: {
alert = {
body = "great match!";
title = "Portugal vs. Denmark";
};
}, gcm.message_id: 0:1464264430528872......]
Here is the entire function called when I receive a message :
func application( application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
print("Notification received: \(userInfo)")
print(userInfo)
// This works only if the app started the GCM service
GCMService.sharedInstance().appDidReceiveMessage(userInfo);
// Handle the received message
// Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
// [START_EXCLUDE]
NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
userInfo: userInfo)
handler(UIBackgroundFetchResult.NoData);
// [END_EXCLUDE]
}
I don't find how to get the alert body and alert title how can I do that ?
userInfo is a dictionary of type [NSObject : AnyObject]. To access the values, use subscripting.
The "aps" key contains a dictionary which contains a dictionary, so, for example, you could do:
if let aps = userInfo["aps"] as? [String:[String:String]],
alert = aps["alert"],
body = alert["body"],
title = alert["title"] {
print(title)
print(body)
}
Related
I have implemented FCM notification to send notification.
When I send a notification from the firebase console then I get the notification. But when I send the notification from the server, I get success but the notification doesn't appear..
Secondly, how do I get the notification response data?
My didReceiveRemoteNotification function is also not working. Here is my code:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
print("Recived: \(userInfo)")
print("success")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
print("Recived: \(userInfo)")
print("success")
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("Recived: \(userInfo)")
print("success")
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
self.application(application, didReceiveRemoteNotification: userInfo) { (UIBackgroundFetchResult) in
print("Recived: \(userInfo)")
print("success")
}
print("Recived: \(userInfo)")
print("success")
let state: UIApplicationState = application.applicationState
// user tapped notification while app was in background
if state == .inactive || state == .background {
print("Inactive")
}
else {
print("Active")
}
}
I found that the message below works best for me both with Android and iOS (I use a firebase function to send it but that shouldn't matter). Note that for this to work you need to use the send method and not one of the deprecated ones, so something like: admin.messaging().send(message)
Message to feed to the send:
const message = {
token: addresseePushToken,
/*
Apple Push Notification Service
https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns
*/
apns: {
headers: {
'apns-priority': 10, // defaults to 10 (send immediately), can be set to 5 (consider device power)
'apns-collapse-id': chatId // group multiple notifications into a single notification (must not exceed 64 bytes)
},
payload: {
aps: {
alert: {
title: title,
body: doc.message,
},
badge: unseenCount+1,
}
}
},
/*
Android specifics
https://firebase.google.com/docs/cloud-messaging/admin/send-messages#android_specific_fields
*/
android: {
priority: 'high', // either 'normal' or 'high' - high send immediately & waking sleeping device
notification: { // notification object creates status bar notification if app in background
tag: chatId, // key for grouping notifications (Android only)
title: title,
body: doc.message,
color: '#ffffff', // notification's icon color
icon: thumbUrl, // if not specified FCM displays the launcher icon as defined in app manifest
}
},
// data object is available to the app both when received in foreground and background
data: {
type: 'chat',
senderId: senderId,
title: title,
body: doc.message,
},
}
I solved an issue very similar to this adding this to the Firebase notification:
"apns" : {
"payload" : {
"aps" : {
"content-available" : 1
}
}
}
I'm trying to setup silent push notifications and I'm stuck with this problem. JSON that I send to APNs is:
{
"aps": {
"alert": "test",
"badge": 0,
"content-available": 1
}
}
Delegate method is:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
var pushData = userInfo["aps"] as? [AnyHashable : Any];
if pushData?["content-available"] as? Int == 1 {
NSLog("received silent notification")
completionHandler(.noData)
} else {
NSLog("received notification")
completionHandler(.newData)
}
}
When backend sends push notification, my app is in background. XCode shows me 'received silent notification', but this notification still pops up as normal. Could you please tell me, what I'm doing is wrong? It probably shouldn't be happening, right?
Project is set up with 'remote notifications' checked in 'background modes'.
Correct json payload for silent push notification should look like this
{
"aps" = {
"content-available" : 1,
"sound" : ""
};
// add custom key-value pairs
}
I need some help in getting the inner push data.
in my app delegate file i have the following:
// Push notification received
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
// Print notification payload data
print("Push notification received: \(data)")
}
which results in the following:
Push notification received: [AnyHashable("aps"): {
Link = "http://www.website.com.dll?i.user8=App&id=374941&mobileOnly=true";
alert = "New survey";
}]
How would i go about in getting the value go LINK from data
thanks
You can get push data as like below.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
let dictPayload = userInfo as NSDictionary
if let data = dictPayload.value(forKey: "aps") as? NSDictionary {
let link = data.value(forKey: "Link") as? String
print(link)
}
print("Push notification received: \(data)")
}
I need to store the last alert json string value coming from a Push Notification.
Using "aps: \(userInfo["aps"]!)" returns the whole json:
aps: {
alert = "last alert message";
}
but I only need "last alert message".
This is my code:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
dUserInfo = userInfo
print("aps: \(userInfo["aps"]!)")
NSUserDefaults.standardUserDefaults().setObject(dUserInfo, forKey: "last_push")
NSUserDefaults.standardUserDefaults().synchronize()
}
How can I get the message properly?
Try this:
if let aps = userInfo["aps"] as? [String: AnyObject] {
if let alert = aps["alert"] as? String {
NSUserDefaults.standardUserDefaults().setObject(alert, forKey: "last_push")
}
}
We are using kinvey business logic to push notification on postSave:
Here is what it look like if sent from engagements:
{
"aps": {
"alert": "Hello World",
"sound": "default"
},
}
The aps structure is passed as a nested object on the userInfo object that is available in the didReceiveRemoteNotification appDelegate method. You can use it with something like this in Swift:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
if let notification = userInfo["aps"] as? NSDictionary,
let alert = notification["alert"] as? String {
// do something with 'alert'
(code snippet from http://www.intertech.com/Blog/push-notifications-tutorial-for-ios-9/)