Phonegap推送插件设置

时间:2015-10-20 10:37:19

标签: android cordova phonegap-plugins phonegap-pushplugin

我正在使用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

感谢您的帮助

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:1)

对于找到答案的人来说,目前最好的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中,打开项目“功能”部分下的“推送通知”服务。还要确保从Google Developer Console for Android和Apple Push Notification服务的p12文件以及iOS到服务器团队的密码共享正确的Api密钥,以避免配置漏洞。