In android we can make alert message of push notification dynamically on conditions(data) which comes in push notification payload. i.e piece of code gets executed when push comes and then alert is shown.
Cant we do this in ios? Do we need to send alert message from API all the time? cant we change it on client side(ios)?
-
We can not change the message of the push notification form client side. apple does not allow. For this you have to use local notification only.Ashish Kakkad– Ashish Kakkad2015年07月04日 06:33:24 +00:00Commented Jul 4, 2015 at 6:33
1 Answer 1
In fact, you can. You just need to send a "silent" remote notification, handle the notification in your app and display local notifications depending on the payload. The steps are:
Implement
didReceiveRemoteNotification:fetchCompletionHandler:Make sure to register for remote notifications, see documentation here:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; return YES; }Also make sure to edit
Info.plistand check the "Enable Background Modes" and "Remote notifications" check boxes:enter image description here
Additionally, you need to add
"content-available":"1"to your push notification payload, otherwise the app won't be woken if it's in the background (see documentation here):For a push notification to trigger a download operation, the notification’s payload must include the content-available key with its value set to 1. When that key is present, the system wakes the app in the background (or launches it into the background) and calls the app delegate’s application:didReceiveRemoteNotification:fetchCompletionHandler: method. Your implementation of that method should download the relevant content and integrate it into your app
So payload should at least look like this:
{ aps = { "content-available" : 1, sound : "" }; }
Just leave the sound property empty and omit the alert/text property and your notification will be silent.
Unfortunately, the app won't be woken up, if it's not running at all (force-quit), see this answer.
Comments
Explore related questions
See similar questions with these tags.