Cordova/Phonegap iOS解析推送插件

10

我花费了很多时间找到适用于Android和iOS平台的正确的cordova推送通知插件。

我的要求是:

  1. 能够接收parse推送通知(在Android和iOS中)
  2. 能够将所有传入的推送通知存储在移动本地存储SQLite中。

我已经尝试过所有适用于Android和iOS平台的以下parse推送cordova插件:

  1. https://github.com/avivais/phonegap-parse-plugin
  2. https://github.com/taivo/parse-push-plugin
  3. https://github.com/campers/parse-push-plugin
  4. https://github.com/manishiitg/parse-push-plugin

对于Android:以上所有插件都可以完美地满足我的上述要求。

对于iOS:只有第一个插件,即https://github.com/avivais/phonegap-parse-plugin可以使用。但是,我无法将通知保存在本地存储SQLite中。这意味着只有我的第一个要求被满足,但我的第二个要求没有被满足。

所有其他插件的github页面(即第2、3、4个)都说明:

“请注意,我只在这个分支的Android方面工作。iOS方面还没有更新。”

是否有任何插件可用于同时满足Android和iOS平台的两个要求?

(或者)

如果没有通用插件可用,则如何将传入的插件存储在iOS sqlite中?

请帮帮我。提前致谢。


2
请说明您为什么要对这个问题进行负投票? - Sivakumar
2
很奇怪有四个用户踩了这篇文章却没有说明原因。请解释一下。 - Mr_Green
1
@Mr_Green 是的,人们应该有责任心解释为什么他们对一篇帖子进行了负面评价。 - Strikers
嗨, 如果你正在使用ionic进行开发,你可以考虑:http://docs.ionic.io/docs/push-overview。 它有很好的文档。 - Rahul
@Rahul,这和解析推送通知一样吗?我们知道解析推送在推送通知方面非常受欢迎。它可以处理数千个通知而不会有任何负载。但是Ionic推送刚刚进入alpha版。我不确定它是否能够智能地处理多个通知。 - Sivakumar
显示剩余2条评论
3个回答

4

我偶然成为了https://github.com/taivo/parse-push-plugin的维护者。

看起来你接触到的是我刚开始维护的版本。当时,源代码库好像很久没有更新了,并且那个时候我只关注于安卓方面的问题。自那之后,我已经提供了完整的iOS支持。这个插件同时适用于parse-serverparse.com。而且,我还改进了安装过程,使其变得更加简单方便。

cordova add https://github.com/taivo/parse-push-plugin

需要写一些 config.xml 标签来指示服务器url和应用程序id。

这样就可以避免在设置插件时手动处理Android清单、Java和Objective C时的繁琐操作。

现在,只需在javascript中设置一个事件处理程序,即可接收推送通知并存储到sqlite中。请务必使用某种设备就绪或平台就绪的事件处理程序来包装它,以确保插件已正确加载。

$ionicPlatform.ready(function(){
    if(window.ParsePushPlugin){
       ParsePushPlugin.on('receivePN', function(pn){
           console.log('yo i got this notif:' + JSON.stringify(pn) );

           //
           // do your sqlite storage here
           //
       });
    }
});

1
你可能会对Azure推送通知感兴趣。它将Push通知服务结合在一起,因此您可以从一个中心点向两个设备发送消息。
我引用:

通知中心是一种可扩展的跨平台解决方案,用于向移动设备发送推送通知,与Cordova应用程序配合使用效果良好。通知中心管理每个PNS的注册。更重要的是,通知中心让您创建模板注册,因此您可以只使用一行代码向所有已注册的设备发送消息,而无需考虑平台。您还可以使用标签仅向具有特定注册的设备发送定向通知。有关通知中心的更多信息,请参见Azure网站aka.ms/nkn4n4。

这里我有一个辅助类,用于注册您的设备到推送通知服务。要发送推送通知,您可以使用Azure门户并以json格式发送样式化的推送通知。
var Pushman = {

    Initialize: function (hubConnString, hubName, gcmSenderId, callbackRegistered, callbackUnRegistered, callbackInlineNotification, callbackBackgroundNotification, callbackError) {

        //store connection and callback information on app startup for Push Registration later
        Pushman.HubConnectionString = hubConnString;
        Pushman.HubName = hubName;
        Pushman.GcmSenderId = gcmSenderId;

        //callbacks
        Pushman.RegisteredCallback = callbackRegistered;
        Pushman.UnRegisteredCallback = callbackUnRegistered;
        Pushman.NotificationForegroundCallback = callbackInlineNotification;
        Pushman.NotificationBackgroundCallback = callbackBackgroundNotification;
        Pushman.ErrorCallback = callbackError;

    },

    RegisterForPushNotifications: function (tags) {
        //setup Azure Notification Hub registration
        Pushman.Hub = new WindowsAzure.Messaging.NotificationHub(Pushman.HubName, Pushman.HubConnectionString, Pushman.GcmSenderId);
        Pushman.Hub.registerApplicationAsync(tags).then(Pushman.onRegistered, Pushman.onError);

        //setup PushPlugin registration
        Pushman.Push = window.PushNotification;
        var push;

        //register depending on device being run
        if (device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos") {

            //android

            push = Pushman.Push.init(
                 { "android": { "senderID": Pushman.GcmSenderId } }
            );
            push.on('registration', Pushman.onRegistered);
            push.on('notification', Pushman.onAndroidNotification);
            push.on('error', Pushman.onError);


        } else {

            //iOS
            push = Pushman.Push.init(
                { "ios": { "alert": "true", "badge": "true", "sound": "true" } }
                );

            push.on('registration', Pushman.onRegistered);
            push.on('notification', Pushman.onIOSNotification);
            push.on('error', Pushman.onError);

        }
    },

    UnRegisterForPushNotifications: function () {

        if (Pushman.Hub != null) {

            //dont pass through error handler

            //unreg azure
            Pushman.Hub.unregisterApplicationAsync()
               .then(Pushman.onUnRegistered, null);

            //unreg native
            Pushman.Push.unregister(Pushman.onUnRegistered, null);

        }

    },

    onRegistered: function (msg) {
        Pushman.log("Registered: " + msg.registrationId);

        //only call callback if registrationId actually set
        if (msg.registrationId.length > 0 && Pushman.RegisteredCallback != null) {
            Pushman.RegisteredCallback(msg);
        }
    },

    onUnRegistered: function () {
        Pushman.log("UnRegistered");

        if (Pushman.UnRegisteredCallback != null) {
            Pushman.UnRegisteredCallback();
        }
    },

    onInlineNotification: function (msg) {
        Pushman.log("OnInlineNotification: " + msg);

        if (Pushman.NotificationForegroundCallback != null) {
            Pushman.NotificationForegroundCallback(msg);
        }
    },

    onBackgroundNotification: function (msg) {
        Pushman.log("OnBackgroundNotification: " + msg);

        if (Pushman.NotificationBackgroundCallback != null) {
            Pushman.NotificationBackgroundCallback(msg);
        }
    },

    onColdStartNotification: function (msg) {
        Pushman.log("OnColdStartNotification: " + msg);

        if (Pushman.NotificationBackgroundCallback != null) {
            Pushman.NotificationBackgroundCallback(msg);
        }
    },

    onError: function (error) {
        Pushman.log("Error: " + error);

        if (Pushman.ErrorCallback != null) {
            Pushman.ErrorCallback(error);
        }
    },

    onAndroidNotification: function (e) {

        switch (e.event) {
            case 'registered':

                if (e.regid.length > 0) {
                    Pushman.onRegistered("Registered");
                }
                break;

            case 'message':

                if (e.foreground) {

                    //if this flag is set, this notification happened while app in foreground
                    Pushman.onInlineNotification(e.payload.message);

                } else {

                    //otherwise app launched because the user touched a notification in the notification tray.
                    if (e.coldstart) {
                        //app was closed
                        Pushman.onColdStartNotification(e.payload.message);
                    }
                    else {
                        //app was minimized
                        Pushman.onBackgroundNotification(e.payload.message);
                    }
                }
                break;

            case 'error':
                Pushman.onError(e.msg);
                break;

            default:
                Pushman.onError("Unknown message");
                break;
        }
    },

    onIOSNotification: function (event) {

        //TODO: not sure how ios works re cold start vs inline msg types?

        if (event.alert) {
            navigator.notification.alert(event.alert);
        }
        if (event.badge) {
            Push.setApplicationIconBadgeNumber(app.successHandler, app.errorHandler, event.badge);
        }
    },

    tokenHandler: function (result) {
        // iOS - not sure its use though appears somewhat important

        // 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.
        alert('device token = ' + result);

    },

    log: function (msg) {
        console.log(msg);
    },

}

///"class" variables - not sure how to put them into the js "class"
Pushman.Push = null;
Pushman.Hub = null;
Pushman.HubConnectionString = null;
Pushman.HubName = null;
Pushman.GcmSenderId = null;
Pushman.NotificationForegroundCallback = null;
Pushman.NotificationBackgroundCallback = null;
Pushman.RegisteredCallback = null;
Pushman.UnRegisteredCallback = null;
Pushman.ErrorCallback = null;

我并没有亲自编写这个,所有功劳归于这位
然后,你只需要在应用程序启动时初始化插件:
//azure notificationshub connection information
notificationHubPath = "notificationhub name";
connectionString = "notificatin hub connectionstring";
//sender id for google cloud services
var senderIdGCM = "sender id from google gcm";
//tag registration (csv string), can be empty but not undefined
var registrationTagsCsv = ""; //test1, test2

var app = {

    Initialize: function () {
        //reg for onload event
        this.AppStart();
    },

    AppStart: function () {
        "use strict";
        document.addEventListener('deviceready', app.onLoad, false);
        document.addEventListener('deviceready', onDeviceReady.bind(this), false);

        function onDeviceReady() {
            // Handle the Cordova pause and resume events
            document.addEventListener('pause', onPause.bind(this), false);
            document.addEventListener('resume', onResume.bind(this), false);

            // TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.
        };

        function onPause() {
            // TODO: This application has been suspended. Save application state here.
        };

        function onResume() {
            // TODO: This application has been reactivated. Restore application state here.
        };
    },

    onLoad: function () {

        app.log("Initializing...");

        //setup push notifications
        Pushman.Initialize(connectionString, notificationHubPath, senderIdGCM,
                           app.onNotificationRegistered, app.onNotificationUnRegistered,
                           app.onNotificationInline, app.onNotificationBackground, app.onNotificationError);

        //hookup cmd buttons
        app.registerForPush();
        //$("#register").click(app.registerForPush);
        //$("#unregister").click(app.unRegisterForPush);

        app.onAppReady();
    },

    registerForPush: function (a, c) {

        app.log("Registering...");
        //register for tags
        Pushman.RegisterForPushNotifications(registrationTagsCsv);

    },

    unRegisterForPush: function (a, c) {

        app.log("UnRegistering...");
        //register for tags
        Pushman.UnRegisterForPushNotifications();

    },

    onAppReady: function () {
        app.log("Ready");
    },

    onNotificationRegistered: function (msg) {
        app.log("Registered: " + msg.registrationId);
    },

    onNotificationUnRegistered: function () {
        app.log("UnRegistered");
    },

    onNotificationInline: function (data) {
        app.log("Inline Notification: " + data);
    },

    onNotificationBackground: function (data) {
        app.log("Background Notification: " + data);
    },

    onNotificationError: function (error) {
        app.log("Error: " + error);
    },

    log: function (msg) {
        console.log(msg);
    },

};

如果您想存储消息,只需将存储代码添加到接收消息的SQL中即可。您需要一个Azure帐户才能使其正常工作,此处您可以获得免费试用版。它将允许您每月免费发送高达100万个推送通知。

巨大的帮助,这是为数不多使用通知中心清晰地解释此问题的帖子之一。 - GONeale

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接