phonegap / phonegap-plugin-push

Register and receive push notifications
MIT License
1.94k stars 1.91k forks source link

App crashes with payload having data and notification attributes when app is in foreground #2577

Open Amitkumar2408 opened 5 years ago

Amitkumar2408 commented 5 years ago

(Android) Device vendor : Samsung

Cordova CLI version and cordova platform version

cordova --version                                    # 8.0.0
cordova platform version android                     # 6.3.0

Plugin version # 2.1.3

Sample Push Data Payload

Sample #1

{
    "data": {
        "alertName": "Product Expiry",
        "notification": ["Banner", "Badge"]
    },
    "priority": "high",
    "time_to_live": 1,
    "registration_ids": ["xxxx"]
}

Sample #2

{
    "data": {
        "messageID": "12345",
        "alertName": "Sample Alert",
        "expiry": "1",
        "alertTitle": "",
        "priority": "3",
        "updateBy": "Joe",
        "updatedTime": ""
    },
    "notification": {
      "body": "FCM Message body",
      "title": "FCM Message test"
    },
    "priority": "high",
    "time_to_live": 1,
    "registration_ids": [xxxx"]
}

Sample Code that illustrates the problem

pushSetup(){
        const options: PushOptions = {
            android: {
                senderID: '719866444444'
            },
            ios: {
                alert: 'true',
                badge: true,
                sound: 'false'
            }
        };

        const pushObject: PushObject = this.push.init(options);

        pushObject.on('notification').subscribe(
            (notification: any) => {
                console.log(notification);
            }
        );

        pushObject.on('registration').subscribe((registration: any) => {
            console.log('Device registered', registration);
        });

        pushObject.on('error').subscribe(error => {
            console.error('Error with Push plugin', error);
        });
    }
Bonythomasv commented 5 years ago

The issue happens even when we send Data payload alone. the word notification inside the Data payload seems to be causing a null pointer exception. Which is contributing towards the app crash. sample payload. { "data": { "notification": ["Banner", "Badge"] }, "priority": "high", "time_to_live": 1, "registration_ids": [""] } FYI, notification is custome payload we uses in our push Alert https://fcm.googleapis.com/fcm/send API call.

Yuripetusko commented 5 years ago

Same issue on same version of android and plugin after recently re-installing the plugin, worked fine before with same version which is strange.

ElD4n1 commented 5 years ago

Hi, I have the same issue and want to provide my information to resolve this.

Background: I am working on a cross platform solution with platforms Android, iOS and Browser. For Android and iOS I am using phonegap-push-plugin and for the Browser I am using the firebase node library directly. The browser needs the data payload when the website is not open, otherwise the background service worker will not be notified..thats why I need it in particular.

A temporary workaround would be to send a different payload to native devices than to the desktop browser, but I would prefer a single solution here.

However, here my setup: The exeption (which results immediately after the reception of the notification and only when the app is in foreground, when it is in background the notification gets shown correctly in status bar without a crash):

Application attempted to call on a destroyed WebView
    java.lang.Throwable
        at org.chromium.android_webview.AwContents.f(SourceFile:314)
        at org.chromium.android_webview.AwContents.a(SourceFile:288)
        at org.chromium.android_webview.AwContents.q(SourceFile:544)
        at amV.loadingStateChanged(SourceFile:159)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:325)
        at android.os.Looper.loop(Looper.java:142)
        at android.app.ActivityThread.main(ActivityThread.java:6703)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)

Implementation:

  async initializeNativePush() {
    var enabled = await this.push.hasPermission();
    if(enabled) {
      console.log('We have permission to send push notifications');
    } else {
      console.log('We do not have permission to send push notifications');
    }

    // to initialize push notifications
    const options: PushOptions = {
      android: {
        topics: ['company_announcement']
      },
      ios: {
        topics: ['company_announcement'],
        alert: 'true',
        badge: true,
        sound: 'false'
      },
      windows: {},
      browser: {
        pushServiceURL: 'http://push.api.phonegap.com/v1/push',
      }
    }

    const pushObject: PushObject = this.push.init(options);
    pushObject.on('notification').subscribe((notification: any) => console.log('Received a notification', notification));
    pushObject.on('registration').subscribe((registration: any) => console.log('Device registered', registration));
    pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
  }

Payload:

{
    "to": "/topics/company_announcement",
    "notification":
    { 
        "title": "Test", 
        "body": "Test" 
    },
    "data":
    {
        "notification":
        {
            "title": "Test", 
            "body": "Test",
            "click_action": "http://url.com"
        }
    }
}

The relevant installed cordova packages: (note that firebase is only used on web browser and is a pure javascript/node library)

"cordova-android": "7.1.1",
"cordova-browser": "5.0.4",
"cordova-ios": "4.5.5",
"cordova-plugin-compat": "^1.2.0",
"cordova-plugin-device": "^2.0.2",
"cordova-plugin-ionic-keyboard": "^2.1.3",
"cordova-plugin-ionic-webview": "^2.2.0",
"cordova-plugin-ms-adal": "^0.10.1",
"cordova-plugin-splashscreen": "^5.0.2",
"cordova-plugin-statusbar": "^2.4.2",
"cordova-plugin-whitelist": "^1.3.3",
"cordova-support-google-services": "^1.2.1",
"firebase": "^5.5.6",
"phonegap-plugin-multidex": "^1.0.0",
"phonegap-plugin-push": "2.2.3",

My cordova config in package.json:

"plugins": {
  "cordova-plugin-splashscreen": {},
  "cordova-plugin-statusbar": {},
  "cordova-plugin-device": {},
  "cordova-plugin-ms-adal": {},
  "cordova-plugin-whitelist": {},
  "cordova-plugin-ionic-webview": {},
  "cordova-plugin-ionic-keyboard": {},
  "phonegap-plugin-push": {
    "FCM_VERSION": "11.8.0",
    "ANDROID_SUPPORT_V13_VERSION": "27.+"
  },
  "cordova-support-google-services": {}
},
"platforms": [
  "android",
  "ios"
]

My specified gradle dependencies:

  dependencies {
      implementation fileTree(dir: 'libs', include: '*.jar')
      // SUB-PROJECT DEPENDENCIES START
      implementation(project(path: ":CordovaLib"))
      implementation "com.android.support:support-annotations:27.+"
      implementation "com.android.support:support-v13:27.+"
      implementation "me.leolin:ShortcutBadger:1.1.17@aar"
      implementation "com.google.firebase:firebase-messaging:11.8.0"
      // SUB-PROJECT DEPENDENCIES END
  }

The resulting gradle dependencies (note that i already worked on bringing all libs to the same version, in particular I changed firebase libs to 11.8.0 to comply with google play services and also the android support libs which had different versions too):

debugRuntimeClasspath - Dependencies for runtime/packaging
+--- com.microsoft.aad:adal:1.12.0@aar
+--- com.android.support:multidex:1.0.2@aar
+--- :CordovaLib (variant: debug)
+--- com.android.support:support-v13:27.1.1@aar
+--- com.android.support:appcompat-v7:27.1.1@aar
+--- com.google.firebase:firebase-messaging:11.8.0@aar
+--- com.google.firebase:firebase-core:11.8.0@aar
+--- com.google.firebase:firebase-analytics:11.8.0@aar
+--- com.google.firebase:firebase-analytics-impl:11.8.0@aar
+--- com.google.firebase:firebase-iid:11.8.0@aar
+--- com.google.firebase:firebase-common:11.8.0@aar
+--- com.google.android.gms:play-services-tasks:11.8.0@aar
+--- com.google.android.gms:play-services-basement:11.8.0@aar
+--- com.android.support:support-v4:27.1.1@aar
+--- com.android.support:support-fragment:27.1.1@aar
+--- com.android.support:animated-vector-drawable:27.1.1@aar
+--- com.android.support:support-core-ui:27.1.1@aar
+--- com.android.support:support-core-utils:27.1.1@aar
+--- com.android.support:support-vector-drawable:27.1.1@aar
+--- com.android.support:support-media-compat:27.1.1@aar
+--- com.android.support:support-compat:27.1.1@aar
+--- com.android.support:support-annotations:27.1.1@jar
+--- me.leolin:ShortcutBadger:1.1.17@aar
+--- com.google.code.gson:gson:2.2.4@jar
+--- com.google.firebase:firebase-messaging-license:11.8.0@aar
+--- com.google.firebase:firebase-iid-license:11.8.0@aar
+--- com.google.android.gms:play-services-basement-license:11.8.0@aar
+--- com.google.firebase:firebase-common-license:11.8.0@aar
+--- com.google.firebase:firebase-analytics-license:11.8.0@aar
+--- android.arch.lifecycle:livedata-core:1.1.0@aar
+--- android.arch.lifecycle:viewmodel:1.1.0@aar
+--- android.arch.lifecycle:runtime:1.1.0@aar
+--- com.google.android.gms:play-services-tasks-license:11.8.0@aar
+--- com.google.firebase:firebase-analytics-impl-license:11.8.0@aar
+--- android.arch.lifecycle:common:1.1.0@jar
+--- android.arch.core:runtime:1.1.0@aar
\--- android.arch.core:common:1.1.0@jar

I hope this helps resolving this issue.