【问题标题】:Cordova/Phonegap iOS Parse-Push PluginCordova/Phonegap iOS Parse-Push 插件
【发布时间】:2015-08-09 00:27:13
【问题描述】:

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

我的要求是:

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

我已经尝试了以下所有适用于 Android 和 iOS 平台的 parse push 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 页面(即 2nd、3rd、4th)声明:

“请注意,我只处理了这个 fork 的 Android 方面。iOS 方面尚未更新。”

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

(或)

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

请帮助我。提前致谢。

【问题讨论】:

  • 请给出你拒绝投票这个问题的原因?
  • 奇怪的是,有四个用户对这篇文章投了反对票,但没有说明原因。请注意解释。
  • @Mr_Green 是的,人们应该有足够的责任来解释他们为什么拒绝投票
  • 您好,如果您使用 ionic 构建,您可以考虑:docs.ionic.io/docs/push-overview。它有很好的文档。
  • @Rahul,和解析推送通知一样吗?正如我们所知,解析推送对于推送通知非常流行。它可以处理数以千计的通知而没有任何负载。但离子推刚刚来到阿尔法。我不确定它是否可以通过 smart 处理多个通知?

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


【解决方案1】:

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

我引用:

通知中心 用于发送推送的可扩展、跨平台解决方案 通知移动设备,通知中心与 科尔多瓦应用程序。通知中心管理每个人的注册 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】:

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

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

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

【讨论】:

    【解决方案3】:

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

    看起来你在我刚出生时就抓住了我的叉子。当上游分支似乎停滞了一段时间时,我选择了它,当时我只是在解决 Android 方面的问题。从那时起,我就提供了完整的 iOS 支持。它适用于parse-server 以及即将发布的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
               //
           });
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-28
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 2020-04-22
      相关资源
      最近更新 更多