2015-10-20 81 views
0

im工作在phonegap v5.3.6和cordova v5.3.3。我在README上做了一切,但插件无法正常工作。Phonegap推送插件安装

这是我的代码如下;

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"); 
     }); 
    } 

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

这里是插件的混帐回购协议:https://github.com/phonegap/phonegap-plugin-push

谢谢您的帮助

回答

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); 
} 
+0

[Push Plugin](https://github.com/phonegap-build/PushPlugin)已弃用,所以我使用[this one](https://github.com/phonegap/phonegap-plugin-push )。 另外一个没有为我工作。 –

+0

您可能有其他问题。我已经使用https://github.com/phonegap-build/PushPlugin。请在评论中输入你的错误。 – Dhruv

+0

我试图弄清楚会发生什么,但现在它会抛出一个错误,即“Class not foud。”。我搜索了一个修复程序,我尝试了所有的建议,但我失败了。我认为这些插件没有工作,因为config.xml文件。我会在修复config.xml文件后通知您。谢谢btw。 –

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推送通知服务的p12文件以及iOS的密码向服务器团队分享正确的Api密钥以避免配置漏洞。