简体   繁体   中英

Display push notification as alert message on iOS app [phonegap/cordova][Pushwoosh]

Hi thanks for viewing this question, I am having trouble receiving my push notification when my iOS app is open. It works perfect when the app is running in the background though. But when the app is open It is not even firing the 'push-notification' event that should display the notification as an alert message.

I followed these instructions, but made a few tweaks: http://www.pushwoosh.com/programming-push-notification/ios/ios-additional-platforms/push-notification-sdk-integration-for-phonegap/

and here is my code when the page loads:

document.addEventListener("deviceready", pushwooshReady, true);
document.addEventListener("push-notification", displayPushwooshMessage, true);

pushwooshReady function:

function pushwooshReady() {
    initPushwoosh();

    if(device.platform == 'iOS') {
        app.receivedEvent('deviceready');
    }
}

the initPushwoosh function:

function initPushwoosh() {
    //get the plugin
    var pushNotification = window.pushNotification;

    if(device.platform == 'iOS') {
        //call the registration function for iOS
        registerPushwooshIOS();
    } else if (device.platform == 'Android') {
        //call the registration function for Android
        registerPushwooshAndroid();
    }
    pushNotification.onDeviceReady();
}

and this is the registerPushWooshIOS function:

function registerPushwooshIOS() {
    var pushNotification = window.pushNotification;

    //register for push notifications
    pushNotification.registerDevice({alert:true, badge:true, sound:true, pw_appid: PW_appid, appname: PW_appname},
                                function(status) {
                                //this is a push token
                                var deviceToken = status['deviceToken'];
                                console.warn('registerDevice: ' + deviceToken);

                                //we are ready to use the plugin methods
                                onPushwooshiOSInitialized(deviceToken);
                                },
                                function(status) {
                                console.warn('failed to register : ' + JSON.stringify(status));
                                navigator.notification.alert(JSON.stringify(['failed to register ', status]));
                                });

    //reset badges on application start
    pushNotification.setApplicationIconBadgeNumber(0);
}

and here is my displayPushwooshMessage function:

function displayPushwooshMessage(event) {
if(device.platform == 'Android') {

    var msg = event.notification.title;
    var userData = event.notification.userdata;

    if(typeof(userData) != "undefined") {
        console.warn('user data: ' + JSON.stringify(userData));
    }

    alert(msg);

} else if(device.platform == 'iOS') {
    var notification = event.notification;
    alert(notification.aps.alert);
    pushNotification.setApplicationIconBadgeNumber(0);
}
}

Your help will be very much appreciated.

Okay, Looks like I'm going to answer my own question again.

I made it work but didn't use the javascript's alert() method 'cause the the instructions on the pushwoosh website is not really working for me. So what I did was very simple, but took me a day to figure out because i don't have any Objective-C background.

I commented out the if statement inside the handlePushReceived function in PushNotificationManager.m . Here is the code:

    NSString *alertMsg = [pushDict objectForKey:@"alert"];

    bool msgIsString = YES;
    if(![alertMsg isKindOfClass:[NSString class]])
        msgIsString = NO;

    //if(!isPushOnStart && showPushnotificationAlert && msgIsString) { <- Commented this out
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.appName message:alertMsg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        alert.tag = ++internalIndex;
        [pushNotifications setObject:userInfo forKey:[NSNumber numberWithInt:internalIndex]];
        [alert show];
        return YES;
    //} <- Also this one.

    [self processUserInfo:userInfo];

And it worked. I don't even know why it's not working, it is from the SDK i downloaded. Oh well, I'm not sure if anyone has or will ever experience the same problem, but I hope this answer will help those who will in the future.

**UPDATE:

Actually, the error is different and the solution above will not be needed. I received the following reply from the Pushwoosh support team:

Dmitry Dyudeev (Pushwoosh)
May 29 16:41

Hello Clint,

Thank you for contacting us!

It turns out that you have located a bug in our plugin. Thank you for pointing it out! We will >update our plugin in the nearest future.

Meanwhile, since updating the plugin will take some time, you can find the onDeviceReady method in the PushNotification.m file and add the following line at its end, it will solve the issue:

  [[NSUserDefaults standardUserDefaults] synchronize]; 

Please let me know about the results!

Regards,
Dmitry Dyudeev
Pushwoosh Team

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