简体   繁体   中英

Phonegap push notification does not appear in Status bar nor in Lock screen for iOS using the PushPlugin plugin

Intro

I'm using Phonegap 4.2 (based on Cordova 5.0) to create a cross-platform app.

For iOS I'm using Xcode 6.0 and I'm using the PushPlugin Cordova plugin to handle Push Notifications.

My Problem

I am able to receive push notifications within the app in the iOS version, but when the app is running in the background then I do not receive any push notifications and they do not appear in the status bar nor in the lock screen. By background I mean when the app is closed.

Details

Referencing

I include the PushPlugin plugin in the config.xml properly:

<feature name="PushPlugin">
    <param name="android-package" value="com.plugin.gcm.PushPlugin" />
    <param name="ios-package" value="PushPlugin" />
</feature>

I'm referencing the PushPlugin JavaScript object properly in the index.html file:

<script type="text/javascript" src="./js/PushNotification.js"></script>

Attaching the push-notification event

I have properly attached the notification event to the method onNotificationAPN:

pushNotification = window.plugins.pushNotification;
if (device.platform == 'android' || device.platform == 'Android' || device.platform == 'amazon-fireos')
{
    // ...
}
else
{
    pushNotification.register(tokenHandler, errorHandler, 
    {
        "badge":"true",
        "sound":"true",
        "alert":"true",
        "ecb":"onNotificationAPN"
    });
}

tokenHandler and errorHandler are defined and so is onNotificationAPN ;

function onNotificationAPN(e)
{
    // handle APNS notifications for iOS
    if (e.alert)
    {
        // showing an alert also requires the org.apache.cordova.dialogs plugin
        // Note that I have org.apache.cordova.dialogs aswell
        navigator.notification.alert(e.alert);
        // This code snippet runs fine when the app is open: the app receives the push notification and it's alerted to the user.
    }
    if (e.sound)
    {
        // playing a sound also requires the org.apache.cordova.media plugin
        // Note that I have org.apache.cordova.media plugin aswell
        var snd = new Media(e.sound);
        snd.play();
    }
    if (e.badge)
    {
        pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
        // This code snippet works fine when the app is open: the app receives a push notification and when I close the app the badge count is set to 1, whether that's an expected behavior or not I'm not sure but not what matters right now. 
    }
}

As said, the app does receive and alerts notifications pushed to it when the app is open. The device does not seem to notice the push, however, when the app is not open. I'd expect the push notification to appear in the lock screen and or in the status bar.

The testing device

I'm testing on an iPad which's OS version is 7.0.3.

Provisioning profile

I'm using a development provisioning profile and the device I'm using for testing has been added to the App's devices in the apple development center.

The push-notification's payload

The payload that's being sent in the Push Notifications looks like this:

Msg: {
    "sound":"beeb.wav",
    "alert":"Here is a testing push notification",
    "badge":"1",
    "location":"", // Custom variable
    foreground:"1"
}

I've tried changing the foreground variable to 0 and replace foreground with background but it doesn't really change anything.

Notification Center

I have configurated the notification center for the app as it should be:

  • Badge App Icon is ON.
  • Sounds is ON.
  • Show in Notification Center is ON.
  • Include is set to 5 Recent Items.
  • Show on Lock Screen is ON.

Help?

I've been looking around a lot but I'm sort of blank, I'd appreciate if Stack-overflow can help. I usually stick to answering questions but now it's my turn to ask :)

The push notification payload need an aps key, and an alert with the message that will be displayed:

For each notification, compose a JSON dictionary object (as defined by RFC 4627). This dictionary must contain another dictionary identified by the key aps. The aps dictionary can contain one or more properties that specify the following user notification types

  • An alert message to display to the user
  • A number to badge the app icon with
  • A sound to play

More info

Payload example:

  {

    "aps" : {

        "alert" : "You got your emails.",

        "badge" : 9,

        "sound" : "bingbong.aiff"

    },

    "acme1" : "bar",

    "acme2" : 42

}

When the app is in foreground you receive the whole payload and you can handle it even if it doesn't have that format, but when the app is in backgroud or closed, the system needs the aps key and the alert with the message that will be shown on the notification center.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM