phonegap / phonegap-plugin-push

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

Receive Notifications in Background #93

Closed christocracy closed 9 years ago

christocracy commented 9 years ago

This plugin is unable to receive notifications while in the background since it doesn't implement application:didReceiveRemoteNotification:fetchCompletionHandler:

Discussion

Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in >the foreground, the system calls this method when your app is running in the foreground or background

Clearly this logic in didReceiveRemoteNotification is useless, since that method is only called in the foreground.

    if (appState == UIApplicationStateActive) {   // <-- Will always be true.
        PushPlugin *pushHandler = [self getCommandInstance:@"PushNotification"];
        pushHandler.notificationMessage = userInfo;
        pushHandler.isInline = YES;
        [pushHandler notificationReceived];
    } else {
        //save it for later
        self.launchNotification = userInfo;
    }
rastermax commented 9 years ago

@macdonst THANKS for the work on this feature.

I updated the plugin through cordova rm plugin and cordova add plugin, but from what I see (im new to cordova dev), the version i get is not the latest from git ? is there a normal delay between versions here and the ones we get from cordova add plugin ?

thanks in advance and again thanks for taking the time to improve this plugin.

PS: i figured out I should i use : cordova plugin add https://github.com/phonegap/phonegap-plugin-push ?

gongfan99 commented 8 years ago

I am curious about a question of GCM payload. For example, if I need to show notification in the shade when app is in background, this plugin requires the payload to be like:

{
    "to" : registrationId,
    "data" :
    {
        "title" : "Portugal vs. Denmark",
        "message" : "Data: please check score",
        "myOwnData": "blablabla"
    }
}

However, per "Hybrid messages with both notification and data payload" section of Google's official website: https://developers.google.com/cloud-messaging/concept-options

It mentions "notification" and "data" fields. And "title", "message" should be put in the "notification" so that Android will show them in the shade for background app. My speculation is that this plugin modifies the default Android behavior so that "title", "message" can be put in "data" field. Is this correct?

macdonst commented 8 years ago

@rastermax if you do an add directly from the git url you'll get the current development version. It may or may not be stable.

@gongfan99 the latest version of the plugin should work with data/notification in the same payload.

deceadmin commented 8 years ago

Hi, I am not able to run the use-case #3 mentioned above. In "onnotification" function I'm saving message to local database. For both Android and iOS (and for both app is running in background and closed completely) notification alert is handled by OS and when user taps the message my code is running (saves to db). But, if I directly open app (not by clicking the notification), nothing happens (onnotification event is not called). I've tried combinations of content-available and other payload data in the examples. What is the point I am missing? I'm using ionic service to send notification messages.

screen shot 2016-01-03 at 21 24 55
ravimandaladev commented 8 years ago

background notification not working on android

barocsi commented 8 years ago

I do receive the notification however when it opens the application the push notification wont work instead: Error in Success callbackId: PushNotification1215169404 : TypeError: Cannot read property 'id' of undefined

macdonst commented 8 years ago

@barocsi then you need to look at where you access 'id' in your on('notification') event handler.

barocsi commented 8 years ago

Thanks @macdonst the problem was on my side.

boyfunky commented 8 years ago

has anyone been able to receive notifications in background on android? i cant seem to catch it at all and i always get an error when i do this as well

var pushPlugin = window.PushPlugin;
pushPlugin.on('notification', function(notification) {

})

var pushPlugin = window.PushNotification; 
pushPlugin.on('notification', function(notification) {

})

All not working and all give me the same error, pushPlugin.on('notification') is not a function. How do i call the phonegap-plugin-push from my deviceready event?

slorber commented 8 years ago

PushNotification.init(cfg).on("notification",...) ?

@boyfunky just try the hello world example first maybe?

https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/EXAMPLES.md

gongfan99 commented 8 years ago

@boyfunky make sure you run "cordova plugin add phonegap-plugin-push" before you run "cordova build"

boyfunky commented 8 years ago

@gongfan99 yes i have already done that. @deceadmin how did you solve ur issue. I am having the same exact issue.

rastermax commented 8 years ago

hi, maybe that can help. Recently I cleared my server code to send a notification and reading the docs about payload and what appears when app is off or in background here is what my server (nodejs) code looks like:

var notifMessage = new gcm.Message({ collapseKey: "MyAppGeneral", delayWhileIdle: false, timeToLive: 3, //weeks // what is shown notification: { title: "SpotiQuiz Message", icon: "icon", body: "THIS IS A TEST" }, //attached payload data: { nico1: "secret data1", nico2: "secret data2" } });

var regTokens = [obj.android_push_id]; //destination user push id

var sender = new gcm.Sender(GOOGLE_CLOUD_MESSAGING_KEY); //your GOOGLE PUSH KEY FOR YOUR APP

sender.send(notifMessage, {registrationTokens:regTokens}, function(err,response){ if(err){ console.log("notifUser:ERROR:Could not send android notification"); } else { console.log("notifUser:NotifSents:"+response); } });

Hope it can help.

On Tue, Mar 1, 2016 at 10:31 PM, Kingsley Simon notifications@github.com wrote:

@gongfan99 https://github.com/gongfan99 yes i have already done that. @deceadmin https://github.com/deceadmin how did you solve ur issue. I am having the same exact issue.

— Reply to this email directly or view it on GitHub https://github.com/phonegap/phonegap-plugin-push/issues/93#issuecomment-191037725 .

deceadmin commented 8 years ago

@boyfunky, i could not solve it

asinha08 commented 8 years ago

For Android it so doesn't work when app is in background.

Is there any alternate plugin. which do not have such issues?

ShaunBrassell commented 8 years ago

I finally solved my background android issue! I would receive fine when app in the foreground, and would happily receive the tray notification when in the background, but my push.on('notification', function(data) {} would not fire at all in the background .... which is what I really wanted as I want to activate the GPS without the user having to click the tray notification. I knew I had to set content_available = 1 ... BUUUUT it is content-available and NOT content_available!! Silly mistake, but I think the "content_available": true, example in the docs threw me!

Hope that helps someone! (viewing the android debug logs helped me pinpoint the issue)

pravinsa commented 7 years ago

Hello All I receive notification in background but while tap on notification, i am unable to get data. One more thing i am using FCM for sending notification

bfx commented 7 years ago

No event-handler called with background app on iOS (it works on android), using GCM/FCM.

var message = new gcm.Message({
    priority: "high", //needed when using FCM > iOS
    data: {
      title: "data title",
      message: "data message",
      "content-available": 1,
    },
   //otherwise no notification is received on iOS
   notification: { title: "title", body: "message"},
    // as documented in PAYLOAD.md
   aps: {
      "content-available": 1,
    },
    notId: 42
});

Is documentation here updated? https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#background-notifications-1

The "aps" object seem totally ignored on iOS ( aps.alert present or missing has no effects)

christocracy commented 7 years ago

Does your iOS app have the background "notification" entitlement in the .plist? -- Snet form Gmail Mobile

bfx commented 7 years ago

It's a ionic app, and on xcode I've enabled the remote notifications in Background Modes. I've "remote-notifications" in UIBackgroundModes in the info.plist.

coderroggie commented 7 years ago

@bfx This may be a bit dated but I'm working through the same documentation now on version 2.0.x version of the code. I believe the docs you are referring to above referencing the payload are in fact the docs for the APNS version of the API. If I understand it properly, node-gcm sends to google who turns around and sends to APNS for you. https://firebase.google.com/docs/cloud-messaging/http-server-ref indicates you should likely be sending something like:

var message = new gcm.Message({
    priority: "high", //needed when using FCM > iOS
    data: {
      title: "data title",
      message: "data message",
    },
   notification: { title: "title", body: "message"},
   content-available: 1,                  
   notId: 42
});

Note the content-available:1 is no longer a child of aps. I'm assuming (but don't know for sure) that fcm turns around and sends something like you see documented in Payload to APNS.

Hope that helps, but as with most things, YMMV.

Cheerio.

vovikdrg commented 7 years ago

@coderroggie btw content-available should be "content_available": true,

lock[bot] commented 6 years ago

This thread has been automatically locked.