Show notification when app is opened from icon

Comments

5 comments

  • Avatar
    Nikita Ovchinnikov

    Hello Gary,

    As far as i understood your use-case, you want your app to show push notification when the application is opened. Unfortunately, Apple notification politics forbids that kind of action. iOS platform allows data transfer to the app only if push notification have been opened, this is obligatory condition.

    However, there is a way to workaround this issue but it is not totally reliable and have some restrictions. You can add “content-available”:1 parameter to the “aps” dictionary in your push payload. Provide this key with a value of 1 to indicate that new content is available. Including this key and value means that when your app is launched in the background or resumed, application:didReceiveRemoteNotification:fetchCompletionHandler: is called.

    If you do so, there is will be a shown push and payload data transfer to the application. You only should find a way for the application to process received content, e.g. you can save the notification text when it is received, then call getLaunchNotification() (gets a payload of a notification, if the app was opened via notification, otherwise returns null) and, if it returns null, check if there is any saved notification and draw an alert with a this content if there is one; then clear saved data to prevent further notification creation.

    Please take a note that workaround mentioned above will not work if notification payload will not be passed to an application, e.g. when it is terminated, so you can lose some or all of the content you transferring if it happens.

    Regards, Nikita.

  • Avatar
    Gary Fenton

    Hi Nikita. Thank you for taking the time to explain. I'm actually developing the Android version first. I will use your iOS solution in 2 weeks time. Is there a solution for Android please? Does Android have the equivalent to {“content-available”:1} or is there a simpler solution because Google are more chilled than Apple? ;-)

  • Avatar
    Nikita Ovchinnikov

    Gary,

    Talking about Android platform.

    You could use onPushReceived, notification delivery callback. Its called every time application receives remote or local notification. Also, you should make your custom notificationFactory to alert application through the onPushReceived callback that push was received. Use the same logic, that was described in iOS example above, i.e check if app is opened with notification or not, using getLaunchNotification. Please take note that Notification Center should be cleared if push notification was not opened, but its content was already used.

    Regards, Nikita.

  • Avatar
    Gary Fenton

    Thank you again, Nikita. Are there JavaScript equivalents to onPushReceived or creating a custom notificationFactory? I'm using PhoneGap so the methods are limited to the plugin provided.

  • Avatar
    Nikita Ovchinnikov

    Gary, as far as i know there is no a way to do this on PhoneGap. Such functionality is available only in the native.

Please sign in to leave a comment.