是否可以在离子框架中发送推送通知

时间:2015-12-11 12:08:05

标签: angularjs ionic-framework

是否可以在离子框架中发送推送通知。任何代码都可用

2 个答案:

答案 0 :(得分:2)

你也可以使用像这样的插件:https://github.com/appfeel/cordova-push-notifications#automatic-installation(你必须实现服务器代码来注册用户设备令牌并在每个案例上发送通知)

cordova plugin add cordova-push-notifications

然后在你的javascript代码中:

var pushNotification, successHandler, tokenHandler, errorHandler;

document.addEventListener("deviceready", function () {
    var token;

    pushNotification = window.plugins.pushNotification;
    if (pushNotification) {
        // Token usually saved in window.localStorage see https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
        token = "recover_saved_token_here";
        startPushNotifications(token);
    }
});

function startPushNotifications(token) {
    var registerOptions;

    if (device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos") {
        registerOptions = {
            "senderID":"replace_with_sender_id",
            "token": "the_user_device_token_empty_first_time"
            "ecb":"onNotificationGCM" // This function is called as window function
        };
    } else if (device.platform == 'iOS') {
        successHandler = tokenHandler; // We need to save the token
        registerOptions = {
            "badge":"true",
            "sound":"true",
            "alert":"true",
            "ecb":"onNotificationAPN" // This function is called as window function
        };
    } else {
        // Not implemented
    }
    pushNotification.register(successHandler, errorHandler, registerOptions);
}

successHandler = function (result) {
    console.log("Push success callback: ", result);
};

errorHandler = function (error) {
    console.log("Push error callback: ", error);
};

你需要实现回调。 Android和亚马逊的:

window.onNotificationGCM = function onNotificationGCM(data) {
    var soundfile,
        mediaSound;

    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 in token parameter.
            // Token usually saved in window.localStorage see https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
            console.log("User device token (android/amazon): " + 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 (parseInt(e.foreground, 0)) {
            // on Android soundname is outside the payload. 
            // On Amazon FireOS all custom attributes are contained within payload
            soundfile = e.soundname || e.payload.soundmMedia;

            // Requires cordova Media plugin
            if (soundfile && Media) {
                mediaSound = new Media('/android_asset/www/sounds/' + soundfile);
                mediaSound.play();
            }

        } else { // otherwise we were launched because the user touched a notification in the notification tray.
            if (e.coldstart) {
                // coldstart notification: the app was running in background (exited with home button or app switched)
            } else {
                // background notification: the app was not running (exited with back button)
            }
        }

        // android only: e.payload.msgcnt
        // amazon-fireos only: e.payload.timeStamp
        break;

    case 'error':
        console.log("Error: " + e.msg);
        break;

    default:
        console.log("Unknown event: " + e.event);
        break;
    }
};

和iOS的:

tokenHandler = function tokenHandler(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.
    // Token usually saved in window.localStorage see https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
    console.log("User device token (iOS): ", JSON.stringify(result));
};

window.onNotificationAPN = function onNotificationAPN(e) {
    var mediaSound;

    if (parseInt(e.foreground, 0) && e.sound && Media) {
        // playing a sound also requires the org.apache.cordova.media plugin
        mediaSound = new Media("sounds/" + e.sound);
        mediaSound.play();
    }
};

答案 1 :(得分:0)

绝对是。 Ionic文档介绍了此过程(可用here),并提供了代码示例,以说明最常见的推送任务(发送/接收)是如何完成的。还有一些教程(就像这个one)显示了使用推送通知的实际应用程序。