phonegap / phonegap-plugin-push

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

LED, Sound, and Vibration do not work on Android 8.0 using recommended "data" payload #2426

Open roland83 opened 6 years ago

roland83 commented 6 years ago

Expected Behaviour

Actual Behaviour

Reproduce Scenario (including but not limited to)

Sending push message via FCM - https://fcm.googleapis.com/fcm/send

Place all message attributes into "data" key of the payload, per plugin readme: https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#notification-vs-data-payloads

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

Android 8.0.0

(Android) What device vendor (e.g. Samsung, HTC, Sony...)

Samsung Galaxy Note 8

Cordova CLI version and cordova platform version

cordova -version: 8.0.0 cordova platform: android 7.1.0

Plugin version

2.2.3

Sample Push Data Payload

{ "registration_ids": ["$deviceId"], "data": { "title": "Notification", "body": "hello world" "soundname": "default", "ledColor": [ 255, 255, 128, 16], "vibrationPattern": [250, 1000, 250, 500] } }

When the "title" and "body" portions of the payload are moved into a "notification" key, then the notification sound and vibration (pattern ignored) work on Android 8.0.0. However, adding a notification key breaks all 3 indicators (sound, vibration, and LED) on Android 6.0.1

Sample Code that illustrates the problem

const push = PushNotification.init({ android: { sound: true, vibrate: true}, ios: { alert: true, badge: true, sound: true } }); push.on('notification', data => { console.log(data); });

macdonst commented 6 years ago

@roland83 you are running into the notification vs data problem which is documented:

https://github.com/phonegap/phonegap-plugin-push#what-is-this https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#notification-vs-data-payloads

Move everything from notification and into data and you should be okay.

roland83 commented 6 years ago

@macdonst I already tried that as noted in the issue. Moving everything into data does not help. The LED and vibration does not work no matter how I arrange my payload.

macdonst commented 6 years ago

@roland83 make sure you don't even put a notification section in your push payload. Can you reproduce the issue while running adb logcat | grep Push?

roland83 commented 6 years ago

@macdonst In fact, putting everything into data makes the problem worse. When I split the payload between notification and data, my Android 8.0.0 device plays the notification sound.

When I put everything into data the notification will come through but there is no sound, LED Indication or vibration.

Here is the payload with everything in the data field (except the device ID, as that results in a failure response from FCM with no message sent. There is no "notification" field at all. { "to": "dg4l_EUJBQ.....",
"data": { "title": "Test Notification", "body": "hello", "soundname": "default", "ledColor": [255, 255, 128, 16], "vibrationPattern": [250, 1000, 250, 500] } }

If I'm doing something wrong please help me understand. Appreciate your quick reply to the issue.

roland83 commented 6 years ago

@macdonst I attempted to use adb logcat while troubleshooting yesterday but struggled to get output. Do you have a link that provides more details on it's use?

macdonst commented 6 years ago

@roland83 yeah, we'll need some logs to figure it out.

https://developer.android.com/studio/command-line/logcat

roland83 commented 6 years ago

@macdonst Thank you. I got logcat working (my issue yesterday was writing the output to a file, no trouble at all piping output to grep)

I notice at the end of the log snippet, vibrate=null, sound=null, color=0 which seems consistent with the behavior. However that output is from Samsung's EdgeLightingManager. Even so, could it indicate that there something wrong with my payload? I sent the same one as listed above.

adb logcat | grep Push (output from Note 8 device, Android 8.0.0) 06-15 10:58:06.479 18669 19875 D Push_FCMService: onMessage - from: 769424599932 06-15 10:58:06.482 18669 19875 D Push_FCMService: sender id = 769424599932 06-15 10:58:06.483 18669 19875 D Push_FCMService: normalize extras 06-15 10:58:06.483 18669 19875 D Push_FCMService: key = soundname 06-15 10:58:06.483 18669 19875 D Push_FCMService: replace key soundname with sound 06-15 10:58:06.484 18669 19875 D Push_FCMService: key = body 06-15 10:58:06.484 18669 19875 D Push_FCMService: replace key body with message 06-15 10:58:06.484 18669 19875 D Push_FCMService: no locale found for key = message, error Value hello of type java.lang.String cannot be converted to JSONObject 06-15 10:58:06.484 18669 19875 D Push_FCMService: key = title 06-15 10:58:06.484 18669 19875 D Push_FCMService: replace key title with title 06-15 10:58:06.484 18669 19875 D Push_FCMService: no locale found for key = title, error Value Test of type java.lang.String cannot be converted to JSONObject 06-15 10:58:06.484 18669 19875 D Push_FCMService: key = vibrationPattern 06-15 10:58:06.484 18669 19875 D Push_FCMService: replace key vibrationPattern with vibrationPattern 06-15 10:58:06.485 18669 19875 D Push_FCMService: key = ledColor 06-15 10:58:06.485 18669 19875 D Push_FCMService: replace key ledColor with ledColor 06-15 10:58:06.485 18669 19875 D Push_FCMService: background 06-15 10:58:06.485 18669 19875 D Push_FCMService: message =[hello] 06-15 10:58:06.485 18669 19875 D Push_FCMService: title =[Test Notification] 06-15 10:58:06.485 18669 19875 D Push_FCMService: contentAvailable =[null] 06-15 10:58:06.485 18669 19875 D Push_FCMService: forceStart =[null] 06-15 10:58:06.485 18669 19875 D Push_FCMService: create notification 06-15 10:58:06.486 18669 19875 E Push_FCMService: Number format exception - Error parsing notId: s == null 06-15 10:58:06.492 18669 19875 D Push_FCMService: Using channel ID = PushPluginChannel 06-15 10:58:06.492 18669 19875 D Push_FCMService: Using channel ID = PushPluginChannel 06-15 10:58:06.496 18669 19875 D Push_FCMService: stored icon=null 06-15 10:58:06.496 18669 19875 D Push_FCMService: stored iconColor=null 06-15 10:58:06.496 18669 19875 D Push_FCMService: stored sound=true 06-15 10:58:06.496 18669 19875 D Push_FCMService: stored vibrate=true 06-15 10:58:06.497 18669 19875 D Push_FCMService: no icon resource found - using application icon 06-15 10:58:06.502 18669 19875 D Push_FCMService: create actions: with in-line 06-15 10:58:06.508 3415 3415 D EdgeLightingManager: showForNotification : isInteractive=false, isHeadUp=false, color=0, sbn = StatusBarNotification(pkg=com.entrologic.Eatza user=UserHandle{0} id=0 tag=Eatza key=0|com.entrologic.Eatza|0|Eatza|10252: Notification(channel=PushPluginChannel pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x11 color=0x00000000 number=0 vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0))

roland83 commented 6 years ago

Here's the data that the plugin is passing up to the Javascript layer in the notification event

.on('notification', data => { console.dir(data) }); Object > additionalData: coldstart: false foreground:true ledColor: (4) [255, 255, 128, 16] vibrationPattern: (4) [250, 1000, 250, 500] Object > message: "test" sound: "default" title: "Test Notification"

roland83 commented 6 years ago

@macdonst I have some additional details:

Removing the "notification" key from the payload gets everything to work as expected on Android 6.0.1. The sound is played, vibration works, LED is flashing and an entry is created in the notification bar.

On my Note 8 (Android 8.0.0), removing the notification key from the payload gives me an entry in the notification bar (with icon, title, and body) as well as a badge number on the app icon. But I do not get a sound, vibration, or LED indicator.

roland83 commented 6 years ago

Partial workaround: When sending a push message to Android 8.0.0, including the "notification" key in the payload gets the sound to play and the device to vibrate (only tested with default sounds so far and the vibration pattern seems to be ignored)

To get vibration to work I had to manually enable it via Settings -> Notifications -> (App) -> Miscellaneous*

Yet to find any way to get the LED indicator to work.

When sending a push message to Android 6 (and presumably 7) with all parameters in the "data" portion of the payload, everything Just Works (TM)

*Miscellaneous seems to be one of the default notification channels (or categories as the Android UI called them). I also had a "PhoneGap PushPlugin" category but changing the settings here had no effect on my notifications. The docs mention the plugin creates this message channel by default.

macdonst commented 6 years ago

@roland83 Okay, I'm glad you got it working for an Android 6 device. I cannot stress enough how important it is not to include a notification area in your payload. That will just bypass this plugin altogether and use the OS to display the push. None of the options you provide in the data section of the payload will be read.

With the Android 8 device I'm going to have to retest. There might be an issue with the default message channel as you pointed out. I will get to this as soon as I can but right now I'm travel for a conference talk.

roland83 commented 6 years ago

@macdonst Thanks again for your help

Message channels don't seem to be working as expected for me. For example I created a new channel and included the android_channel_id in my payload but I'm not getting any of the defined behavior for that channel.

Also, the plugin always creates the default channel, even though I'm calling PushNotification.init in the success callback of createChannel. My understanding was that plugin wouldn't create the default channel if you defined your own before calling init. (Edit: The API docs day the default channel is always created. I read about it not being created was from a 3rd party site)

I will open a new issue with further details.

macdonst commented 6 years ago

@roland83 sorry, I can't reproduce this. I'm wondering if it is something Samsung is over-riding with regards to push messages.

Stefano1964 commented 5 years ago

Hi, i've the same problem (plugin version 2.1.2) Huawei p10 lite with android 7, led and vibration working Huawei Mate 10 pro with Android 9, no led vibration defualt Xiaomi xiaomi mi a2 lite with Android 9 , no led vibration default

Stefano1964 commented 5 years ago

I dont see how you can set led color and vibration pattern server side via payload (Android 8 and 9) as the default channel is created on first launch of the app, simply you cant. I made it work by hard coding led color and vibration pattern (plugin version 2.1.2, for now i stay on cordova android 6.3.0):

  @TargetApi(26)
  private void createDefaultNotificationChannelIfNeeded(JSONObject options) {
    // only call on Android O and above
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      final NotificationManager notificationManager = (NotificationManager) cordova.getActivity()
          .getSystemService(Context.NOTIFICATION_SERVICE);
      List<NotificationChannel> channels = notificationManager.getNotificationChannels();
      if (channels.size() == 0) {
        NotificationChannel mChannel = new NotificationChannel(DEFAULT_CHANNEL_ID, "PhoneGap PushPlugin",
            NotificationManager.IMPORTANCE_DEFAULT);
        mChannel.enableVibration(options.optBoolean(VIBRATE, true));
        mChannel.enableLights(true);
        mChannel.setLightColor(0xFF0000FF);
        mChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
        mChannel.setShowBadge(true);
        notificationManager.createNotificationChannel(mChannel);
      }
    }
  }
andreszs commented 5 years ago

@Stefano1964, the createChannel method does not include any params whatsoever related to the LED or its color. Therefore, setting the LED color via payload is, I'm afraid, not only perfectly valid, but actually the only way.

Stefano1964 commented 5 years ago

@andreszs the createChannel it's Android and yes you can set LED Color in creating a channel SDK Reference

andreszs commented 5 years ago

@Stefano1964 Right, after inspecting the PushPlugin.java file I see that indeed it can set the LED color, however this is not documented, how can we know the possible values/format for this parameter?

Stefano1964 commented 5 years ago

The format it's argb