【问题标题】:Display push notification as alert message on iOS app [phonegap/cordova][Pushwoosh]在 iOS 应用程序 [phonegap/cordova][Pushwoosh] 上显示推送通知作为警报消息
【发布时间】:2014-05-28 10:17:59
【问题描述】:

您好,感谢您查看此问题,当我的 iOS 应用程序打开时,我无法接收推送通知。但是,当应用程序在后台运行时,它可以完美运行。但是当应用程序打开时,它甚至没有触发应该将通知显示为警报消息的 'push-notification' 事件。

我遵循了这些说明,但做了一些调整: http://www.pushwoosh.com/programming-push-notification/ios/ios-additional-platforms/push-notification-sdk-integration-for-phonegap/

这是我在页面加载时的代码:

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

pushwooshReady功能:

function pushwooshReady() {
    initPushwoosh();

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

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();
}

这是 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);
}

这是我的 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);
}
}

非常感谢您的帮助。

【问题讨论】:

    标签: ios cordova push-notification pushwoosh phonegap-pushplugin


    【解决方案1】:

    好的,看来我又要回答我自己的问题了。

    我让它工作了,但没有使用 javascript 的 alert() 方法,因为 pushwoosh 网站上的说明对我不起作用。所以我做的很简单,但是我花了一天时间才弄明白,因为我没有任何 Objective-C 背景。

    我在 PushNotificationManager.mhandlePushReceived 函数中注释掉了 if 语句。代码如下:

        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];
    

    它奏效了。我什至不知道为什么它不起作用,它来自我下载的 SDK。 哦,好吧,我不确定是否有人遇到过或将遇到同样的问题,但我希望这个答案能帮助那些将来会遇到的人。

    **更新:

    实际上,错误是不同的,不需要上面的解决方案。我收到了 Pushwoosh 支持团队的以下回复:

    Dmitry Dyudeev (Pushwoosh)
    5月29日16:41

    你好克林特,

    感谢您与我们联系!

    事实证明,您在我们的插件中发现了一个错误。谢谢你指出!我们将>在不久的将来更新我们的插件。

    同时,由于更新插件需要一些时间,您可以在PushNotification.m文件中找到onDeviceReady方法,并在其末尾添加以下行,即可解决问题:

         [[NSUserDefaults standardUserDefaults] synchronize];
    

    请告诉我结果!

    问候,
    德米特里·杜德耶夫
    Pushwoosh 团队

    【讨论】:

      猜你喜欢
      • 2011-12-19
      • 1970-01-01
      • 2018-07-06
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      相关资源
      最近更新 更多