简体   繁体   English

在iOS应用上显示推送通知作为提醒消息[phonegap / cordova] [Pushwoosh]

[英]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. 嗨,感谢您查看此问题,我在iOS应用程序打开时收到推送通知时遇到问题。 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/ 我按照这些说明进行了一些调整: 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: pushwooshReady功能:

function pushwooshReady() {
    initPushwoosh();

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

the initPushwoosh function: initPushwoosh函数:

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: 这是registerPushWooshIOS函数:

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: 这是我的displayPushwooshMessage函数:

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. 我使它工作但没有使用javascript的alert()方法'因为pushwoosh网站上的说明并不适合我。 So what I did was very simple, but took me a day to figure out because i don't have any Objective-C background. 所以我做的很简单,但是花了一天时间才弄明白,因为我没有任何Objective-C背景。

I commented out the if statement inside the handlePushReceived function in PushNotificationManager.m . 我在PushNotificationManager.m中handlePushReceived函数中注释掉了if语句。 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. 我甚至不知道为什么它不起作用,它来自我下载的SDK。 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: 我收到了Pushwoosh支持团队的以下回复:

Dmitry Dyudeev (Pushwoosh) Dmitry Dyudeev(Pushwoosh)
May 29 16:41 5月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: 同时,由于更新插件需要一些时间,您可以在PushNotification.m文件中找到onDeviceReady方法并在其末尾添加以下行,它将解决问题:

  [[NSUserDefaults standardUserDefaults] synchronize]; 

Please let me know about the results! 请让我知道结果!

Regards, 问候,
Dmitry Dyudeev 德米特里·杜伊耶夫
Pushwoosh Team Pushwoosh团队

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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