apns payload message json format directly shown
Solution 1
You need to get the data from dictionary object and message show on alertview
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (userInfo) {
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Info" message: message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[self.window.rootViewController presentViewController:alert animated:YES completion:nil];
}
}
Solution 2
I think for notification to display data instead of json in push try below json
{"aps":{"alert":"This is your message to be displayed","sound":"default","badge":1}, "Data":{"key1":"val1", "key2":"val2"}}
The alert content will be displayed in push and the Data Key/Val pairs is your custom data that you can pass in payload and access it in your code.
The code shared above is the working code I have used in most of my API projects.
Solution 3
Here while you are generating payload you need to create Array in Python
and than need to apply json
encode and than pass that json string to APNS server.
In PHP generally developer are doing below code:
//Create Payload body with array.
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
//Convert Payload array to JSON
$payload = json_encode($body);
//Pass message to APNS
$msg = chr(o) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
By This we can achieve APNS message in iPhone.
Hope this will help you.
Solution 4
plz use this
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
if(application.applicationState == UIApplicationStateInactive)
{
NSLog(@"Inactive");
//do your things when you click on notification
}
else if (application.applicationState == UIApplicationStateBackground)
{
NSLog(@"Background");
}
else if (application.applicationState == UIApplicationStateActive)
{
NSLog(@"Active");
NSDictionary *aps = [userInfo objectForKey:@"aps"];
NSString *alert = [aps objectForKey:@"alert"]
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:nil
message:alert
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
//do on ok press
}];
[alertController addAction:okAction];
UIViewController *ob = [self getTopViewController];
[ob presentViewController:alertController animated:YES completion:nil];
}
}
-(id)getTopViewController{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
if (![topController isKindOfClass:[NSNull class]]) {
return topController;
}
}
here i am getting top viewcontorller and show alert msg
Solution 5
you need to get the data out from dictionary object like
NSDictionary *aps = [payload objectForKey:@"aps"];
NSString *alert = [aps objectForKey:@"alert"]
Elavarasan
Updated on June 04, 2022Comments
-
Elavarasan almost 2 years
we have implemented apple apns push notification. Configured pem file and device tokens in python. They trigger json payload like this
{"aps" : { "alert" : "got 1 new offer. To view, please tap here." },"notification_type":"New Offer history","redirect_link”:”offers”}.
we got notification, but the full json shows in message. we need to display only alert message only on notification message