Cordova/Phonegap iOS 解析推送插件

Cordova/Phonegap iOS Parse-Push Plugin

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

我的要求是:

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

我已经为 Android 和 iOS 平台尝试了以下所有解析推送 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)指出:

"Please note that I've only worked on the Android aspect of this fork. The iOS side is not yet up to date."

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

(或)

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

请帮助我。提前致谢。

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

我引用:

Notification Hubs A scalable, cross-platform solution for sending push notifications to mobile devices, Notification Hubs works well with Cordova apps. Notification Hubs manages the registrations with each PNS. More important, Notification Hubs lets you create template registrations so you can send messages to all registered devices, regardless of platform, with only a single line of code. You can also use tags to send targeted notifications only to devices with specific registrations. For more information about Notification Hubs, see the Azure Web site at aka.ms/nkn4n4.

这里我有一个帮手 class 可以使用推送通知服务注册您的设备。要发送推送通知,您可以使用 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 万条推送通知。

我认为这篇文章可能有用,它提供了更多让您的混合应用正常工作的直接本机解决方法

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

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

我正好维护https://github.com/taivo/parse-push-plugin

看来你在我的叉子初期就抓住了它。当上游分支似乎停滞了一段时间时,我拿起了它,当时我只是在解决 Android 方面。从那时起,我就提供了全面的 iOS 支持。它适用于 parse-server as well as the out-going parse.com。我也做得更好,让安装变得简单

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

并写了一些 config.xml 标记来指示服务器 url 和应用程序 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
           //
       });
    }
});