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

Receive Notifications in Background #93

Closed
christocracy opened this issue Sep 1, 2015 · 73 comments
Closed

Receive Notifications in Background #93

christocracy opened this issue Sep 1, 2015 · 73 comments

Comments

@christocracy
Copy link

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;
    }
@greg84
Copy link

greg84 commented Sep 1, 2015

I've been debugging this with xcode. The else block is hit when the application is started as a result of the user tapping a notification when the app is not in the foreground (i.e. in the task switcher). In this case the plugin invokes the javascript 'notification' event callback with details of the push message when the init method is called.

Out of interest, do you know if implementing the fetchCompletionHandler variant of this method would allow javascript to run inside the Cordova web view when the app is started in the background?

As a side note, after the app is force quit (double tap home, swipe up) and the device is locked, tapping a notification will bypass the didReceiveRemoteNotification event completely. Although this seems to be by design - see #50.

@christocracy
Copy link
Author

I created a cordova plugin some time ago when using UrbanAirship to receive notifications in the background.

Implementing the fetchCompletionHandler variant does allow Javascript to run when the app is started in the background.

The important thing is that one must allow Javascript to signal the end of the fetchCompletionHandler, since one's push-callback might to an async xhr request to their server.

For example:

var pushPlugin = window.PushNotification;

pushPlugin.on('notification', function(data) {
    // do stuff with data
   .
   .
   .
   // Execute an Async xhr
   $.post({
       url: 'foo.com',
       callback: function() {
           pushPlugin.finish();  // <-- signal to native code that our task is complete.
       }
   });
});

@cmartin81
Copy link

+1 for this feature

@macdonst macdonst added this to the Release 1.3.0 milestone Sep 4, 2015
@macdonst
Copy link
Member

macdonst commented Sep 4, 2015

@christocracy please feel free to send a PR with this functionality.

@christocracy
Copy link
Author

When PhoneGap fixes their plugin and stops including static jar files (eg android-support-v4.jar), breaking other peoples' plugins, I'll consider contributing a PR.

@cmartin81
Copy link

Are there any other good plugins for push I can use that does not break other plugins?

@macdonst
Copy link
Member

macdonst commented Sep 4, 2015

@christocracy I'm in a bit a bind right now as PhoneGap build which I need to support does not support gradle yet. Once it does I already have a commit to make the change. I'd love for you to participate in this plugin. So if you can contribute code that would be great.

@cmartin81 I'd say this one is your best bet and I will resolve this compilation issue as quickly as I can.

@christocracy
Copy link
Author

@macdonst When might PGB support gradle? It won't be soon enough, I'm afraid. I'm so tired of providing support for PGB users of my products.

Until PGB supports gradle, I strongly recommend my customers against using PhoneGap and point them towards more advanced build-services which do support grade, such as CloudFive

@slorber
Copy link
Contributor

slorber commented Sep 4, 2015

@christocracy what exactly do you mean by "receive notifications while in the background"?

Because on this issue I notice I can receive notifications only when the app is in foreground. When the app is closed or not in the foreground, nothing happens at all.

I don't know if this is what this issue is about but for me this is not an enhancement but a bug as it makes the plugin barely unusable :'(

@christocracy
Copy link
Author

For ios in particular, an app can receive a push notification while the app is in the background. iOS can awaken the app in the background and provide exactly 30s of background running-time.

But only if the push-plugin implements the application:didReceiveRemoteNotification:fetchCompletionHandler: method variant

This plugin does not implement this variant thus an iOS app cannot be awakened in the background due to a push request.

An example usage of this feature could be a server which desires to know the geolocation of a device or devices. The server could send a silent "where are you?" push-request with a JSON-body only (e.g.: {"action":"getCurrentPosition"}). The app on the device would awaken, decode the JSON-body and choose to fetch the device's current-position and send it back to the server.

This is how Apple's "Find Friends" app works.

@slorber
Copy link
Contributor

slorber commented Sep 4, 2015

Ok so that seems nice for background sync but it may not be related to my problem :) thanks

@larsar
Copy link

larsar commented Sep 4, 2015

+1

@berndartmueller
Copy link

Does this mean that right now this plugin can not handle push notifications when app is in background?

@christocracy
Copy link
Author

Without a doubt, this plugin can only receive a Push notification while
running in the foreground.

On Saturday, September 5, 2015, Bernd Artmüller notifications@github.com
wrote:

Does this mean that right now this plugin can not handle push
notifications when app is in background?


Reply to this email directly or view it on GitHub
#93 (comment)
.

Snet form Gmail Mobile

@berndartmueller
Copy link

Hmm, is there a simple way to fix this myself? I tried to enable "Remote notifications" but that did not work.

@christocracy
Copy link
Author

No simple way. It needs an obj-c method implemented including some careful handling of the completionHandler callback, as I clearly documented above, including an example usage from one of my plugins.

@berndartmueller
Copy link

Ok, but the deprecated plugin https://github.com/phonegap-build/PushPlugin also does not implement the "application:didReceiveRemoteNotification:fetchCompletionHandler:" method, nevertheless background notifications are working.

@christocracy
Copy link
Author

You're not understanding.

Speaking of iOS, in particular, With your app running in background, you
will see a notification appear, of course. This is done automatically by
the OS.

However, your JavaScript callback is not being executed. What if the push
notification has a JSON payload that your app needs to decode and
respond-to?

The JavaScript callback cannot be executed in the background with this
plugin.

On Saturday, September 5, 2015, Bernd Artmüller notifications@github.com
wrote:

Ok, but the deprecated plugin https://github.com/phonegap-build/PushPlugin
also does not implement the
"application:didReceiveRemoteNotification:fetchCompletionHandler:" method,
nevertheless background notifications are working.


Reply to this email directly or view it on GitHub
#93 (comment)
.

Snet form Gmail Mobile

@berndartmueller
Copy link

Oh sorry, then we are talking about different things. I'm sending push notifications to my unlocked iOS device (app is in background) and would like them to display with a banner. iOS receives the notification but does not display it with a banner (like WhatsApp).

@christocracy
Copy link
Author

If you want to execute a js callback in background due to push, this plugin
won't do it

On Saturday, September 5, 2015, Bernd Artmüller notifications@github.com
wrote:

Oh sorry, then we are talking about different things. I'm sending push
notifications to my unlocked iOS device (app is in background) and would
like them to display with a banner. iOS receives the notification but does
not display it with a banner (like WhatsApp).


Reply to this email directly or view it on GitHub
#93 (comment)
.

Snet form Gmail Mobile

@slorber
Copy link
Contributor

slorber commented Sep 7, 2015

@christocracy I'm trying to understand a little better to know if issue #109 is a duplicate or not

I'm thinking of different usecases:

Receiving notifications in foreground:

No problem here.

Receiving notification in background and notification to the user:

When the user clicks on the notification, the app should open and the notification callback be triggered.

Receiving notification in background, and executing callback while still in background

As far as I understand this is your usecase no? I understand that you want to be able to run js code for 30 seconds inside the Cordova app, without even putting this app in foreground.

I think #109 is more about the 2nd case, as the user mention he "opens the app" while you never open the app in your usecase

@christocracy
Copy link
Author

My concern is use-case #3.

On Monday, September 7, 2015, Sébastien Lorber notifications@github.com
wrote:

@christocracy https://github.com/christocracy I'm trying to understand
a little better to know if issue #109
#109 is a
duplicate or not

I'm thinking of different usecases:
Receiving notifications in foreground:

No problem here.
Receiving notification in background and notification to the user:

When the user clicks on the notification, the app should open and the
notification callback be triggered.
Receiving notification in background, and executing callback while still
in background

As far as I understand this is your usecase no? I understand that you want
to be able to run js code for 30 seconds inside the Cordova app, without
even putting this app in foreground.

I think #109 #109
is more about the 2nd case, as the user mention he "opens the app" while
you never open the app in your usecase


Reply to this email directly or view it on GitHub
#93 (comment)
.

Snet form Gmail Mobile

@alexislg2
Copy link
Contributor

Concern of #109 is indeed use-case 2. #109 is therefore not a duplicate of #93

@AndersNederhoed
Copy link

+1

2 similar comments
@cmellinas
Copy link

+1

@GabrielFerraz
Copy link

+1

chaffeqa added a commit to sportstech/phonegap-plugin-push that referenced this issue Nov 3, 2015
* 'master' of github.com:phonegap/phonegap-plugin-push:
  Version 1.4.2
  Issue phonegap#299: 1.4.x not showing notification on Android
  Version 1.4.1
  Issue phonegap#295: data.registrationId is empty string "" on register event callback
  Issue phonegap#291: Reregister on Android
  [typo] fixing .finish() example
  Update CHANGELOG
  Version 1.4.0
  Issue phonegap#93: Receive Notifications in Background
@rastermax
Copy link

@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
Copy link

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
Copy link
Member

@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
Copy link

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
Copy link

background notification not working on android

@barocsi
Copy link

barocsi commented Jan 24, 2016

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
Copy link
Member

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

@barocsi
Copy link

barocsi commented Jan 25, 2016

Thanks @macdonst the problem was on my side.

@boyfunky
Copy link

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
Copy link
Contributor

slorber commented Feb 25, 2016

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
Copy link

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

@boyfunky
Copy link

boyfunky commented Mar 2, 2016

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

@rastermax
Copy link

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
#93 (comment)
.

@deceadmin
Copy link

@boyfunky, i could not solve it

@asinha08
Copy link

asinha08 commented Apr 4, 2016

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

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

@ShaunBrassell
Copy link

ShaunBrassell commented Apr 22, 2016

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
Copy link

pravinsa commented Jan 4, 2017

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
Copy link

bfx commented Jan 17, 2017

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

  • phonegap-plugin-push@1.9.2
    Using node-gcm to test with this payload (and many other variations):
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
Copy link
Author

christocracy commented Jan 17, 2017 via email

@bfx
Copy link

bfx commented Jan 17, 2017

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
Copy link

@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
Copy link

vovikdrg commented Jun 8, 2017

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

@lock
Copy link

lock bot commented Jun 3, 2018

This thread has been automatically locked.

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

No branches or pull requests