简体   繁体   中英

Push notification not receiving in Cordova Android App

I am not receiving push notifications on my Cordova Android App. With the same code I am able to receive push notifications in my iOS App. Earlier I was using pushwoosh plugin from phonegap build(PGB) :

< gap:plugin name="com.pushwoosh.plugins.pushwoosh" version="3.4.2" />

Push notifications were received both for iOS and Android without any errors.

I recently changed the plugin and added a new one :

< gap:plugin name="pushwoosh-cordova-plugin" version="6.4.0" source="npm" />     

The device gets registered in pushwoosh and obtain device token.
GCM (php-backend) is used to send the push notifications for Android.
I am getting success response from GCM up on sending push notification :

{"multicast_id":86307627949689xxxxx,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1480488792901983%a4d1b626f9xxxxxx"}]}

But the push notification is not received by the Android App in any cases whether the App is running background or foreground or closed.

Reference document : http://docs.pushwoosh.com/docs/cordova-phonegap

My code :

document.addEventListener("deviceready", DeviceReady, false);

function DeviceReady()
{
  window.pushwoosh = cordova.require("pushwoosh-cordova-plugin.PushNotification");

  // should be called before pushwoosh.onDeviceReady
  document.addEventListener('push-notification', function(event) {
     navigator.notification.alert(JSON.stringify(event.notification));  

     //get the notification payload
     if (device.platform == 'android' || device.platform == 'Android') {    
             navigator.notification.alert(event.notification.message);          
    }else{
        navigator.notification.alert(event.notification.aps.alert);             
    }

     if (device.platform == 'iOS') {
        pushwoosh.setApplicationIconBadgeNumber(0);
     }

  });

  // Initialize Pushwoosh. This will trigger all pending push notifications on start.
  // projectid : GOOGLE_PROJECT_NUMBER
  pushwoosh.onDeviceReady({
    appid: "3FXXX-3CXXX",
    projectid: "4631220XXXXX"
  });

  pushwoosh.registerDevice(
    function(status) {
        var pushToken = status.pushToken;
        document.getElementById('hiddentoken').value = pushToken;               
    },function(status) {
        //~ alert("Error Status" + status );
 }); 

  //reset badges on app start
  pushwoosh.setApplicationIconBadgeNumber(0);

}

Manifest :

    <?xml version="1.0" encoding="utf-8" standalone="no"?>
<manifest xmlns:amazon="http://schemas.amazon.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" android:windowSoftInputMode="adjustPan" package="com.XXXXX.XXXXX" platformBuildVersionCode="22" platformBuildVersionName="5.1.1-1819727">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <permission android:name="com.XXXXX.XXXXX.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
    <uses-permission android:name="com.XXXXX.XXXXX.permission.C2D_MESSAGE"/>
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
    <uses-permission android:name="com.sec.android.provider.badge.permission.READ"/>
    <uses-permission android:name="com.sec.android.provider.badge.permission.WRITE"/>
    <uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS"/>
    <uses-permission android:name="com.htc.launcher.permission.UPDATE_SHORTCUT"/>
    <uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE"/>
    <uses-permission android:name="com.sonymobile.home.permission.PROVIDER_INSERT_BADGE"/>
    <uses-permission android:name="com.anddoes.launcher.permission.UPDATE_COUNT"/>
    <uses-permission android:name="com.majeur.launcher.permission.UPDATE_BADGE"/>
    <uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE"/>
    <uses-permission android:name="com.huawei.android.launcher.permission.READ_SETTINGS"/>
    <uses-permission android:name="com.huawei.android.launcher.permission.WRITE_SETTINGS"/>
    <uses-permission android:name="android.permission.READ_APP_BADGE"/>
    <uses-permission android:name="com.oppo.launcher.permission.READ_SETTINGS"/>
    <uses-permission android:name="com.oppo.launcher.permission.WRITE_SETTINGS"/>
    <permission android:name="com.XXXXX.XXXXX.permission.RECEIVE_ADM_MESSAGE" android:protectionLevel="signature"/>
    <uses-permission android:name="com.XXXXX.XXXXX.permission.RECEIVE_ADM_MESSAGE"/>
    <uses-permission android:name="com.amazon.device.messaging.permission.RECEIVE"/>
    <application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:configChanges="locale|keyboard|keyboardHidden|orientation|screenSize" android:label="@string/app_name" android:name="com.XXXXX.XXXXX.Apppp" android:screenOrientation="portrait" android:windowSoftInputMode="adjustUnspecified|stateUnspecified">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
        <meta-data android:name="PW_LOG_LEVEL" android:value="DEBUG"/>
        <meta-data android:name="PW_NO_BROADCAST_PUSH" android:value="false"/>
        <receiver android:exported="true" android:name="com.google.android.gms.gcm.GcmReceiver" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
                <category android:name="com.XXXXX.XXXXX"/>
            </intent-filter>
        </receiver>
        <activity android:exported="false" android:name="com.google.android.gms.common.api.GoogleApiActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
        <receiver android:name="com.pushwoosh.MessageAlertReceiver" android:permission="com.amazon.device.messaging.permission.SEND">
            <intent-filter>
                <action android:name="com.amazon.device.messaging.intent.REGISTRATION"/>
                <action android:name="com.amazon.device.messaging.intent.RECEIVE"/>
                <category android:name="com.XXXXX.XXXXX"/>
            </intent-filter>
        </receiver>
        <service android:name="com.pushwoosh.PushAmazonIntentService"/>
        <amazon:enable-feature android:name="com.amazon.device.messaging" android:required="false"/>
        <activity android:name="com.pushwoosh.richpages.RichPageActivity"/>
        <activity android:name="com.pushwoosh.MessageActivity"/>
        <activity android:name="com.pushwoosh.PushHandlerActivity"/>
        <activity android:name="com.pushwoosh.inapp.WebActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
        <service android:name="com.pushwoosh.inapp.InAppRetrieverService"/>
        <receiver android:enabled="true" android:name="com.pushwoosh.local.BootReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
        <service android:exported="false" android:name="com.pushwoosh.GCMListenerService">
            <intent-filter android:priority="-50">
                <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
            </intent-filter>
        </service>
        <service android:exported="false" android:name="com.pushwoosh.GCMInstanceIDListenerService">
            <intent-filter android:priority="-50">
                <action android:name="com.google.android.gms.iid.InstanceID"/>
            </intent-filter>
        </service>
        <service android:exported="false" android:name="com.pushwoosh.GCMRegistrationService"/>
        <service android:name="com.pushwoosh.location.GeoLocationService"/>
        <activity android:name="com.pushwoosh.internal.utils.PermissionActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
        <receiver android:name="com.pushwoosh.local.AlarmReceiver"/>
        <service android:enabled="true" android:exported="true" android:isolatedProcess="false" android:label="iBeacon" android:name="com.pushwoosh.thirdparty.radiusnetworks.ibeacon.service.IBeaconService"/>
        <service android:enabled="true" android:name="com.pushwoosh.thirdparty.radiusnetworks.ibeacon.IBeaconIntentProcessor"/>
        <service android:name="com.pushwoosh.beacon.PushBeaconService"/>
        <service android:enabled="true" android:exported="false" android:name="com.pushwoosh.internal.utils.LockScreenService"/>
    </application>
</manifest>

Any help will be appreciated.

You may want to try the following as enumerated in Apache Cordova / Phonegap Push Notification Tutorial :

  • Check whether you've installed Push Notification Plugin
  • Make sure, you've installed required tools in SDK Manager
  • Check you've included cordova.js file with your project
  • Check your Sender ID which you're created in GCM Projects
  • Make sure you have internet connection

You can also get more tips and information on the implementation of push notifications in Cordova Application from the following references:

The manifest file only has one receiver, provided by the pushwoosh SDK, this receiver will only be able to show push notification if the notification payload is in the format in which it expects. In case you need to show notifications sent from your own server you need to have your own receiver that should understand the payload you send and then display it.

Note : The latest play services version does not support multiple GCMListenerServices. Ideally you should have one service of your own which understands whether the push is from your server or from pushwoosh and process it accordingly. During your testing you might see instances where the push payload is delivered to multiple GCMListenerServices but this behaviour is not consistent, so best to have only one service

使用Pushbot插件进行cordova app推送通知

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM