PhoneGap通知

时间:2014-12-17 11:42:05

标签: javascript cordova

我试图使用PhoneGap创建一个非常简单的Android / iPhone应用程序。

将推送通知发送到应用的最简单方法是什么,即使它未在用户设备上主动运行?

3 个答案:

答案 0 :(得分:0)

使用push notification phonegap插件:

onNotificationGCM: function(e) {
    switch (e.event) {
        case 'registered':
            if (e.regid.length > 0) {
                console.log("Regid " + e.regid);
                alert('registration id = ' + e.regid);
            }
            break;
        case 'message':
            // this is the actual push notification. its format depends on the data model from the push server
            alert('message = ' + e.message + ' msgcnt = ' + e.msgcnt);
            break;
        case 'error':
            alert('GCM error = ' + e.msg);
            break;
        default:
            alert('An unknown GCM event has occurred');
            break;
    }
}

可以在此处找到完整的使用指南:http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/

答案 1 :(得分:0)

你也可以这样做

JS

// JavaScript Document
var pushNotification;

function onDeviceReady() {
        document.addEventListener("backbutton", function(e) {}, false);
        try {
            pushNotification = window.plugins.pushNotification;
            if (device.platform == 'android' || device.platform == 'Android' || device.platform ==
                'amazon-fireos') {
                pushNotification.register(successHandler, errorHandler, {
                    "senderID": "xxxxxxxxxxxxx",
                    "ecb": "onNotification"
                }); // required!
            } else {
                pushNotification.register(tokenHandler, errorHandler, {
                    "badge": "true",
                    "sound": "true",
                    "alert": "true",
                    "ecb": "onNotificationAPN"
                }); // required!
            }
        } catch (err) {
            txt = "There was an error on this page.\n\n";
            txt += "Error description: " + err.message + "\n\n";
            alert(txt);
        }
    }
    // handle APNS notifications for iOS

function onNotificationAPN(e) {
        if (e.alert) {
            // showing an alert also requires the org.apache.cordova.dialogs plugin
            navigator.notification.alert(e.alert);
        }
        if (e.sound) {
            // playing a sound also requires the org.apache.cordova.media plugin
            var snd = new Media(e.sound);
            snd.play();
        }
        if (e.badge) {
            pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
        }
    }
    // handle GCM notifications for Android

function onNotification(e) {
    switch (e.event) {
        case 'registered':
            if (e.regid.length > 0) {
                // Your GCM push server needs to know the regID before it can push to this device
                // here is where you might want to send it the regID for later use.
                alert("regID = " + e.regid);
            }
            break;
        case 'message':
            // if this flag is set, this notification happened while we were in the foreground.
            // you might want to play a sound to get the user's attention, throw up a dialog, etc.
            if (e.foreground) {
                // on Android soundname is outside the payload.
                // On Amazon FireOS all custom attributes are contained within payload
                var soundfile = e.soundname || e.payload.sound;
                // if the notification contains a soundname, play it.
                // playing a sound also requires the org.apache.cordova.media plugin
                var my_media = new Media("/android_asset/www/" + soundfile);
                my_media.play();
            }
            break;
        case 'error':
            break;
        default:
            break;
    }
}

function tokenHandler(result) {
    alert('device token = ' + result);
    // Your iOS push server needs to know the token before it can push to this device
    // here is where you might want to send it the token for later use.
}

function successHandler(result) {}

function errorHandler(error) {}
document.addEventListener('deviceready', onDeviceReady, true);

HTML

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

参考:http://blog.revivalx.com/2014/08/29/implement-push-notifications-for-android-and-ios-phonegap-part-1/

答案 2 :(得分:0)

您可以使用GCM(Google云消息传递服务)发送推送通知

将PushPlugin https://github.com/phonegap-build/PushPlugin添加到您的项目

您可以从http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/

找到教程