nilesh
nilesh

Reputation: 1336

show alert of push notification dynamically on ios

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)?

Upvotes: 2

Views: 1287

Answers (1)

Baris Akar
Baris Akar

Reputation: 4965

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.plist and 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.

Upvotes: 1

Related Questions