Receiving a Direct Push Notification
The reception handler and the user action handler receive Direct Push notifications.
The following sample code is for getting notification content with the application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
method of the AppDelegate
class.
Swift:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("Received notification : \(userInfo)")
// Create a KiiReceivedMessage instance from userInfo.
// But note that the KiiReceivedMessage class is not used to process Direct Push messages.
let message = KiiReceivedMessage(fromAPNS: userInfo)
// Extract field values set in the developer portal.
let value1 = userInfo["MyKey1"]
let value2 = userInfo["MyKey2"]
print("Value1=\(value1)")
print("Value2=\(value2)")
completionHandler(.newData)
}
Objective-C:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result)) completionHandler {
NSLog(@"Received notification: %@", userInfo);
// Create a KiiReceivedMessage instance from userInfo.
// But note that the KiiReceivedMessage class is not used to process Direct Push messages.
KiiReceivedMessage *message = [KiiReceivedMessage messageFromAPNS:userInfo];
// Extract field values set in the developer portal.
NSString *value1 = [userInfo objectForKey:@"MyKey1"];
NSString *value2 = [userInfo objectForKey:@"MyKey2"];
NSLog(@"Value1=%@", value1);
NSLog(@"Value2=%@", value2);
completionHandler(UIBackgroundFetchResultNewData);
}
In this sample code, the reception handler receives a Direct Push notification.
A KiiReceivedMessage
instance is created in this sample code. However, you can not get meaningful data from the instance for the Direct Push notifications.
Instead, get and use the values that are specified in the developer portal from userInfo
. This sample code assumes that custom data has been set in the MyKey1
key and the MyKey2
key in the developer portal and get and output it to Xcode for debugging.
If your mobile app will receive Direct Push notifications via Notification Center, implement the user action handler as well. As with this sample code, get the values that are specified in the developer portal from userInfo
.
The reception handler and the user action handler might be successively called or called multiple times for a single notification and you might need to develop additional functions such as that for deleting duplicated processes. For understanding when and what methods are called, see Combinations of Reception Methods.
If you have issues in receiving push notifications, information in Troubleshooting might help you. You can also use the simple implementation illustrated in Push Notification Tutorials to investigate if the push notification is working properly.
Determining the type of push notification
The reception handler and the user action handler receive all push notifications to the mobile app.
You can determine which feature (Push to App, Push to User, or Direct Push) generated a push notification that was received on the mobile app by checking keys in the payload. For available fields by feature, see KiiReceivedMessage in the appledoc. Determine the type of push notification according to the information in the appledoc.
Push message example
The following is an example of the data you can get from userInfo
. You can get custom data specified on the developer portal (in this example Key1
= Value1
). For more information, see the appledoc.
{
Key1 = Value1;
aps = {
alert = {
"action-loc-key" = "";
body = "";
"launch-image" = "";
"loc-args" = (
);
"loc-key" = "";
};
sound = "";
};
}