Cordova / Phonegap iOS Parse-Push插件

时间:2015-05-27 05:11:24

标签: ios cordova parse-platform apple-push-notifications

我花了很多时间为Android和Android的解析推送通知找到正确的cordova插件。 iOS平台。

我的要求是:

  1. 接收解析推送通知(在Android和iOS中)
  2. 能够将所有传入的推送通知存储在移动本地存储Sqlite中。
  3. 我已经尝试了所有以下解析push cordova插件,适用于Android和Android。 iOS平台。

    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
    5. 对于Android:以上所有插件都能完美地满足上述要求。

      对于iOS:只有第一个插件,即https://github.com/avivais/phonegap-parse-plugin正在运行。而且我也无法在本地存储sqlite中保存通知。这意味着只有我的第一个要求得到满足,但不是我的第二个要求。

      其余插件的所有github页面(即第2,第3,第4)都声明:

      "请注意,我只使用此fork的Android方面。 iOS方面尚未更新。"

      是否有适用于Android和Android的插件iOS平台满足我的2个要求吗?

      (或)

      如果两个平台都没有通用插件,那么如何在iOS sqlite中存储传入的插件?

      请帮帮我。提前谢谢。

3 个答案:

答案 0 :(得分:4)

我碰巧维持https://github.com/taivo/parse-push-plugin

看起来你抓住我的叉子处于起步阶段。当上游分支暂停一段时间时,我把它拿起来,当时我只是解决了Android问题。从那以后,我提供了完整的iOS支持。它适用于parse-server以及正在进行的parse.com。我也做了一个更好的安装只是

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

并编写一些config.xml标记以指示服务器网址和应用ID。

这应该可以解决设置插件时手动搞乱Android Manifest,Java和Objective C的痛苦。

现在应该达到或超过您的要求。要在sqlite中接收推送通知和存储,您所要做的就是在javascript中设置事件处理程序。请确保使用某种设备就绪或平台就绪事件处理程序来包装它,以确保插件已正确加载。

$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 :(得分:1)

您可能对Azure Push Notifications感兴趣。它结合了推送通知服务,因此您可以从一个中心点向两个设备发送消息。

我引用:

  

通知中心用于发送推送的可扩展的跨平台解决方案   向移动设备发送通知,通知中心可以正常使用   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;

我自己并没有这样写,所有归功于this guy

然后你只需要在应用程序启动时初始化插件:

//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帐户来完成这项工作,here您可以获得免费跟踪。它允许您每月免费发送多达100万个推送通知。

答案 2 :(得分:0)

我认为这篇文章可能有用,它为您的混合应用程序提供了更多直接的本地解决方法

http://www.hiddentao.com/archives/2015/04/10/parse-push-notifications-for-your-android-and-ios-cordova-app/

我正在使用Cordova Android应用程序,这似乎是一个可行的解决方案

相关问题