Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

GCM iOS topic: on('notification') not triggered when app in background #806

Closed
ghost opened this issue Apr 13, 2016 · 15 comments
Closed

GCM iOS topic: on('notification') not triggered when app in background #806

ghost opened this issue Apr 13, 2016 · 15 comments

Comments

@ghost
Copy link

ghost commented Apr 13, 2016

Expected Behaviour

Alert shown on iPhone when receiving push notification with sound and vibration when app is in background.
Note: this works on my Android Samsung S6 Edge

Actual Behaviour

The device seem to receive the push notification (see logs at the bottom) but on('notification') is not triggered.

Reproduce Scenario (including but not limited to)

see payload and sample code below

Steps to Reproduce

see payload and sample code below

Platform and Version iOS 9.2.1, 9.3.1

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

Cordova CLI version and cordova platform version

cordova --version                                     6.1.1
cordova platform version iOS                  4.1.0

Plugin version

cordova plugin version | grep phonegap-plugin-push    1.6.2

Sample Push Data Payload

var message = new gcm.Message({
                                priority: 'high',
                                delayWhileIdle: true,
                                timeToLive: 3,
                                data: {
                                    url: 'infos',
                                    title: 'Whitehorse Now',
                                    body: data.new_val.title,
                                    sound: 'default',
                                    msgcnt: 1,
                                    contentAvailable: '1'
                                }
                            }),
                sender  = new gcm.Sender(config.gcm.api_key);

                sender.send(message, {topic: '/topics/' + topic.topic}, function(err, response) {
                    if (err) {
                        console.error(err);
                    }
                    else {
                        console.log('Push success: ' + JSON.stringify(response));
                    }
                });

Sample Code that illustrates the problem

function _initPush(topics) {
    app.push = PushNotification.init({
        android: {
            senderID: app.config.gcm.senderID,
            sound: "true",
            vibration: "true",
            topics: topics
        },
        ios: {
            senderID: app.config.gcm.senderID,
            gcmSandbox: "true",
            alert: "true",
            badge: "true",
            sound: "true",
            vibration: "true",
            clearBadge: "true",
            topics: topics
        }
    });

    app.push.on('registration', function(data) {
        console.log('Registration ID: ' + data.registrationId);

    });

    app.push.on('notification', function(data) {
        alert('got notification')

        app.push.finish(function() {
            console.log('Processing of push data is finished');
        });
    });

    app.push.on('error', function(e) {
        console.log('PushNotification error: ' + e.message);
    });
}

Logs taken while reproducing problem

2016-04-13 12:08:40.811 YukonNow[369:96113] didReceiveNotification with fetchCompletionHandler
2016-04-13 12:08:40.812 YukonNow[369:96113] app in-active
2016-04-13 12:08:40.812 YukonNow[369:96113] this should be a silent push
2016-04-13 12:08:40.812 YukonNow[369:96113] Push Plugin notId handler
2016-04-13 12:08:40.813 YukonNow[369:96113] Notification received
2016-04-13 12:08:40.814 YukonNow[369:96113] Push Plugin key: content-available

@macdonst
Copy link
Member

@bardu this is the payload you would use on Android:

{
                                priority: 'high',
                                delayWhileIdle: true,
                                timeToLive: 3,
                                data: {
                                    url: 'infos',
                                    title: 'Whitehorse Now',
                                    body: data.new_val.title,
                                    sound: 'default',
                                    msgcnt: 1,
                                    contentAvailable: '1'
                                }
                            }

This is the payload you would use on iOS when using GCM:

{
                                priority: 'high',
                                delayWhileIdle: true,
                                timeToLive: 3,
                                content_available: true,
                                notification: {
                                    url: 'infos',
                                    title: 'Whitehorse Now',
                                    body: data.new_val.title,
                                    sound: 'default',
                                    badge: 1
                                }
                            }

It's a pain to send two different notifications for Android and iOS but that's the best I can do at the moment.

@ghost
Copy link
Author

ghost commented Apr 13, 2016

Leveraging GCM topics the my server doesn't know whether the recipient is Android or iOS, or do I miss something?

@macdonst
Copy link
Member

@bardu you are not missing anything other than to get push to work properly for Android I've had to force everyone to use the data part of the push payload. If you use the notification part the OS takes over and we don't get to have nice things like background notification, action buttons, etc.

You should probably set it up so that your devices subscribe to different topics, say "foo-ios" and "foo-android" then have your server send the data in the right framework to the two different topics.

@DamodarSojka
Copy link

@macdonst I like the idea of using topics to separate iOS and Android users. Is it possible to send notification only to subset of users subscribed to a topic?

@hung-doan
Copy link
Contributor

@macdonst So there is noway to send hybrid msg to GCM ?
I see this guide about Hybrid messages with both notification and data payload
https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages

My msg payload:

{
    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }

But on Android, on('notification') not triggered when app is running in background.

If there is no solution at the moment, Is there road-map to support this feature in next releases?!

@macdonst
Copy link
Member

@hungdoan2 currently if you add the notification part to the payload then the Android OS does not pass on the info to the push plugin. I'm looking into a way to fix it.

@DamodarSojka
Copy link

@macdonst So currently there is no way to get both Android and iOS to work EXCEPT sending different data for iOS and different for Android right?

I am glad to hear that you are working on that, do you have some estimate when this feature will be working? Any estimate will help.

@macdonst
Copy link
Member

@DamodarSojka Correct and no, I can't provide an estimate as it would only be a wild guess at this point.

@badpenguin
Copy link

@macdonst i was having problem receiving push notification while in background, just used your example and it started to work again!

@ghost
Copy link
Author

ghost commented Sep 5, 2016

@DamodarSojka Correct and no, I can't provide an estimate as it would only be a wild guess at this point.

@macdonst Any updates or new thoughts on this?

@ghost
Copy link
Author

ghost commented Oct 4, 2016

You should probably set it up so that your devices subscribe to different topics, say "foo-ios" and "foo-android" then have your server send the data in the right framework to the two different topics.

Since this is a good workaround for the issue I'm closing it.

@ghost ghost closed this as completed Oct 4, 2016
@PDivya
Copy link

PDivya commented Oct 24, 2016

Hi Guys, Is anybody else facing issue with IOS 10.0.2 with push plugin 1.8.3 ? It's not working for me. I am not able to get notification on IOS. Any insight will be helpful. I got stuck with this problem from last 2 days.

Regards,
Divya

@hung-doan
Copy link
Contributor

@PDivya
Is it working with your Android devices?.
Is it working with IOS 9.x ?
Have you tried to use older version of the plugin?

@PDivya
Copy link

PDivya commented Oct 25, 2016

@hung-doan : Yes, it is working with my android device as well as with IOS 9.x. I tired downgrading to 1.8.2 but it didn't help.

@lock
Copy link

lock bot commented Jun 4, 2018

This thread has been automatically locked.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 4, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

5 participants