【问题标题】:Phonegap push plugin setupPhonegap 推送插件设置
【发布时间】:2015-10-20 10:37:19
【问题描述】:

我正在使用 phonegap v5.3.6 和 cordova v5.3.3。我在 README 上做了所有事情,但插件不起作用。

下面是我的代码;

onDeviceReady: function() {
        var push = PushNotification.init({
            "android": {
                "senderID": "MY_SENDER_ID"
            },
            "ios": {},
            "windows": {}
        });
        push.on('registration', function(data) {
            console.log("registration event");
            document.getElementById("regId").innerHTML = data.registrationId;
            console.log(JSON.stringify(data));
        });

        push.on('notification', function(data) {
            console.log("notification event");
            console.log(JSON.stringify(data));
            var cards = document.getElementById("cards");
            var push = '<div class="row">' +
                  '<div class="col s12 m6">' +
                  '  <div class="card darken-1">' +
                  '    <div class="card-content black-text">' +
                  '      <span class="card-title black-text">' + data.title + '</span>' +
                  '      <p>' + data.message + '</p>' +
                  '    </div>' +
                  '  </div>' +
                  ' </div>' +
                  '</div>';
            cards.innerHTML += push;
        });

        push.on('error', function(e) {
            console.log("push error");
        });
    }

它不会抛出任何成功或错误消息。这个代码短语有什么问题?

这里是插件的 git repo:https://github.com/phonegap/phonegap-plugin-push

感谢您的帮助

【问题讨论】:

    标签: android cordova phonegap-plugins phonegap-pushplugin


    【解决方案1】:

    尝试使用Push Notification 链接。它对我很好。

    首先注册您的设备,

    pushNotification.register(
    successHandler,
    errorHandler,
    {
        "senderID":"replace_with_sender_id", //It should be your project id that you will get from Google Developer Console while registering the project with package name.
        "ecb":"onNotification"
    }
    

    并添加两个事件 - 像这样的成功和失败,

    function successHandler (result) {
         alert('result = ' + result); //Here you will get your device id.
    }
    
    
    function errorHandler (error) {
         alert('error = ' + error);
    }
    

    另外,添加 onNotification 事件,当设备收到通知时触发。

    function onNotification(e){
         alert(e.event);
    }
    

    【讨论】:

    • Push Plugin 已弃用,所以我使用的是this one。还有一个对我不起作用。
    • 您可能遇到了其他问题。我用过github.com/phonegap-build/PushPlugin。请在评论中指出您的错误。
    • 我试图弄清楚发生了什么,但现在它抛出了一个错误,即“Class not foud.”。我搜索它的修复程序并尝试了所有建议但我失败了。我认为这些插件因为 config.xml 文件而不起作用。我会在修复 config.xml 文件后通知您。顺便说一句,谢谢。
    • 嗨@DHruv,我尝试做某事然后我成功了,但又出现了一些问题。我向 gcm 发送了注册信息,但我没有回电,我不知道为什么。
    【解决方案2】:

    对于寻找答案的人来说,目前现有的最好的 Phonegap 推送通知插件是这里提到的那个:Phonegap Push Notification Plugin。它适用于所有 Android 和 iOS 版本。可以如下使用:

    var push = PushNotification.init({
                android: {
                    senderID: "XXXXXXXXXXXX",
                },
                ios: {
                    alert: "true",
                    badge: "true",
                    sound: "true",
                }
            });
    push.on('registration', function(data) {
        console.log(data.registrationId);
        registerDeviceToken(data.registrationId);
        });
    
    push.on('notification', function(data) {
        console.log("notification event");
         alert(JSON.stringify(data));
         });
    
    push.on('error', function(e) {
        console.log("push error");
        alert(JSON.stringify(e));
    });
    
    function registerDeviceToken(deviceToken){
    //Register the registrationId or deviceToken to your server as per the webservice type and parameters configuration set
    }
    

    在您的应用程序中触发“DeviceReady”事件后调用上述代码。您的 AndroidManifest 文件中的配置将是(参考目的):

    <application>
    <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
    
            <activity
                android:name="com.adobe.phonegap.push.PushHandlerActivity"
                android:exported="true" />
    
            <receiver android:name="com.adobe.phonegap.push.BackgroundActionButtonHandler" />
            <receiver
                android:name="com.google.android.gms.gcm.GcmReceiver"
                android:exported="true"
                android:permission="com.google.android.c2dm.permission.SEND">
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                    <category android:name="yourPackageName" />
                </intent-filter>
            </receiver>
    
            <service
                android:name="com.adobe.phonegap.push.GCMIntentService"
                android:exported="false">
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                </intent-filter>
            </service>
            <service
                android:name="com.adobe.phonegap.push.PushInstanceIDListenerService"
                android:exported="false">
                <intent-filter>
                    <action android:name="com.google.android.gms.iid.InstanceID" />
                </intent-filter>
            </service>
            <service
                android:name="com.adobe.phonegap.push.RegistrationIntentService"
                android:exported="false" />
        </application>
    <permission
            android:name="${applicationId}.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
    
        <uses-permission android:name="${applicationId}.permission.C2D_MESSAGE"  />
    
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.WAKE_LOCK" />
        <uses-permission android:name="android.permission.VIBRATE" />
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    

    只要安装了插件,这些配置就会自动添加到 AndroidManifest 中。在 iOS 中,打开项目功能部分下的推送通知服务。还要确保将来自 Android 的 Google Developer Console 的正确 Api 密钥和 Apple 推送通知服务的 p12 文件及其 iOS 密码共享给服务器团队,以避免配置漏洞。

    【讨论】:

      猜你喜欢
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      • 2016-12-15
      • 2013-08-27
      相关资源
      最近更新 更多