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

allow forceShow option to always notify (android only atm) #178

Merged
merged 4 commits into from
Oct 2, 2015

Conversation

AdriVanHoudt
Copy link
Contributor

fixes #76

(not sure if done completely right though :D)

@@ -88,7 +88,7 @@ protected void onMessage(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
// if we are in the foreground, just surface the payload, else post it to the statusbar
if (PushPlugin.isInForeground()) {
if (!FORCE_SHOW && PushPlugin.isInForeground()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you be getting this option in a way similar to this?

SharedPreferences prefs = context.getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
boolean force_show = prefs.getBoolean(FORCE_SHOW, false);

Accessing FORCE_SHOW directly is only targeting the String defined in PushConstants, and a String is always true'ish, thus making it not an option to decide on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like you are right ^^ give me a minute to update

@macdonst
Copy link
Member

macdonst commented Oct 1, 2015

@AdriVanHoudt can you add the necessary documentation to the README.md file and then rebase so the PR merges cleanly? They I should be able to merge this.

@AdriVanHoudt
Copy link
Contributor Author

Will do

@AdriVanHoudt
Copy link
Contributor Author

Done

@macdonst
Copy link
Member

macdonst commented Oct 1, 2015

@AdriVanHoudt actually I did some testing of this today and it's not quite ready for merging. Take a look at my comments in the code.

@AdriVanHoudt
Copy link
Contributor Author

@macdonst did you add comments or do you mean the inline ones? Because I don't see any added on github atm

// if we are in the foreground, just surface the payload, else post it to the statusbar
if (PushPlugin.isInForeground()) {
if (!forceShow && PushPlugin.isInForeground()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have two variables we need to account for 4 different cases.

  1. forceShow = false & PushPlugin.isInForeground() = true
  • should set foreground boolean to true and then fire the notification event.
  1. forceShow = true & PushPlugin.isInForeground() = true
  • should set foreground boolean to true, fire the notification event and show the notification in the shade
  1. forceShow = false & PushPlugin.isInForeground() = false
  • should set foreground boolean to false and then show the notification in the shade
  1. forceShow = true & PushPlugin.isInForeground() = false
  • should set foreground boolean to false and then show the notification in the shade

Obviously 3 and 4 are the same so we can handle that in an else but we need something like this un-optimized code:

            if (!forceShow && PushPlugin.isInForeground()) {
                extras.putBoolean(FOREGROUND, true);
                PushPlugin.sendExtras(extras);
            } else if (forceShow && PushPlugin.isInForeground()) {
                extras.putBoolean(FOREGROUND, true);
                PushPlugin.sendExtras(extras);

                // Send a notification if there is a message
                String message = this.getMessageText(extras);
                String title = getString(extras, TITLE, "");
                if ((message != null && message.length() != 0) ||
                        (title != null && title.length() != 0)) {
                    createNotification(context, extras);
                }
            } else {
                extras.putBoolean(FOREGROUND, false);

                // Send a notification if there is a message
                String message = this.getMessageText(extras);
                String title = getString(extras, TITLE, "");
                if ((message != null && message.length() != 0) ||
                        (title != null && title.length() != 0)) {
                    createNotification(context, extras);
                }
            }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't case 2) cause redundant behaviour? The notification event would be possibly triggered twice (one by the receipt of the message, and another by the click handler on the notificaton itself). I'm not saying that's wrong, I just think something else is needed to differ from both events, as both data objects will have the same properties as of right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case 2 is wrong imo, the notification event should only be fired when actually clicked on the notification, as @fredgalvao said this could cause double events atm

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you both mean. Remember that was vaguely pseudo code I posted. I still think we'd need to take care of case 2 when the information pushed to the device does not include a title or message parameter. Perhaps the developer wants to silently push some info to the device and still trigger the notification event. So more like:

            if (!forceShow && PushPlugin.isInForeground()) {
                extras.putBoolean(FOREGROUND, true);
                PushPlugin.sendExtras(extras);
            } else if (forceShow && PushPlugin.isInForeground()) {
                extras.putBoolean(FOREGROUND, true);

                // Send a notification if there is a message
                String message = this.getMessageText(extras);
                String title = getString(extras, TITLE, "");
                if ((message != null && message.length() != 0) ||
                        (title != null && title.length() != 0)) {
                    createNotification(context, extras);
                } else {
                    PushPlugin.sendExtras(extras);
                }
            } else {
                extras.putBoolean(FOREGROUND, false);

                // Send a notification if there is a message
                String message = this.getMessageText(extras);
                String title = getString(extras, TITLE, "");
                if ((message != null && message.length() != 0) ||
                        (title != null && title.length() != 0)) {
                    createNotification(context, extras);
                }
            }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That fixes it well enough for me, but not for all cases... Well, if anything the user can always rely on the notId and check for duplicate events, I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works, my train of thought was that when there is an actual notification shown don't fire the event but, forceShow would not affect this when there is nothing to show
@fredgalvao what cases aren't covered?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure of the practical cases, but in theory one could send a push notification with message === null and still want it to be shown with something else (like a big picture, or a summaryText, or whatever else he might provide other than message).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am correct at least a message is required to show any notification

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, the snippet @macdonst provided already contains the "message OR title" piece.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update today to match these comments

@macdonst
Copy link
Member

macdonst commented Oct 1, 2015

@AdriVanHoudt just finished adding them.

Also, when I had my OnePlusOne with the app running with your code I never got a notification event via JS or anything in the notification shade.

@AdriVanHoudt
Copy link
Contributor Author

Ok updated, only thing different then then the last code @macdonst proposed is that when not in foreground and no title/message it will send the extra data, which it doesn't do now

macdonst added a commit that referenced this pull request Oct 2, 2015
allow forceShow option to always notify (android only atm)
@macdonst macdonst merged commit 64da9a1 into phonegap:master Oct 2, 2015
@AdriVanHoudt AdriVanHoudt deleted the always-notify branch October 2, 2015 22:27
@chriseckman
Copy link

Hey guys, I'm trying to get this to work, however the forceShow option doesn't seem to be working. I've tried to pull down the most recent changes, but it doesn't look like it's working on my N6.

@AdriVanHoudt
Copy link
Contributor Author

It is working for me when pulling directly from github in my config.xml <plugin name="phonegap-plugin-push" spec="https://github.com/phonegap/phonegap-plugin-push" />
Also @macdonst any eta on a release, I don't like having to pull from github directly, maybe do a 1.3.1 or 1.4.0?

@chriseckman
Copy link

@AdriVanHoudt thank you so much, your a god send. I had to add that into the config.xml to get it working. For whatever reason, "cordova plugin add https://github.com/phonegap/phonegap-plugin-push" wasn't pulling it in.

In a side related question, what's the best way of displaying a confirmation popup if the app is in the foreground? I've found that $ionicPopup and confirm() doesn't work, the popup never displays. I was trying to ask the user if they would like to be redirected after receiving the notification, but it doesn't seem like anything is working. So for now, my work-a-around if to just always show a notification even if the device is in the foreground, then if they click the notification it redirects them. Any thoughts?

@AdriVanHoudt
Copy link
Contributor Author

Np :D I do not recoomend it though, why I asked for an eta on a release.

And implementation depends on the app but you do know the notification event only fires when the notification is actually being clicked right? Unless it is only data or no forceShow and in foreground.

@chriseckman
Copy link

@AdriVanHoudt Yes, that's the behavior I was aiming for. I was trying to only redirect to the new view if the user wanted to get to that view. The issue that I'm running into is that confirm() and $ionicPopup aren't displaying a popup when foreground === true, alert works fine so I know it's firing, but I'm not sure what the issue is with why confirm or $ionicPopup don't work. I was wondering if anyone had similar issues or had found something else that works. I was thinking the only other way is to use $watch to fire the $ionicPopup after I change the value of some $rootScope variable.

@AdriVanHoudt
Copy link
Contributor Author

@chriseckman I have no experience with $ionicPopup sorry :/ I redirect to a certain state on notification click and that works

@macdonst
Copy link
Member

macdonst commented Oct 8, 2015

@AdriVanHoudt I'm going to get a 1.4.0 out this month but an exact day is hard to pick right now as I'm at and off site this week and Cordova Face to Face meeting next week.

@AdriVanHoudt
Copy link
Contributor Author

Maybe do a prerelease? I don't want to set my dependency to the GitHub URL that fetches the master branch

@AdriVanHoudt
Copy link
Contributor Author

@macdonst sorry to bug you but any eta on a release? Just can't wait for the new features ^^

@chriseckman
Copy link

Hey guys, another question. Finally got around to working setting up the iOS app with APN and I'm wondering if any has a workaround for the notifications not displaying if the iOS app is in the foreground? Is there another plugin that would work for iOS?

@AdriVanHoudt
Copy link
Contributor Author

you can probably add an option for this as well (not really sure though), see https://github.com/phonegap/phonegap-plugin-push/blob/master/src/ios/AppDelegate%2Bnotification.m#L65 (just a guess though)

@macdonst
Copy link
Member

@AdriVanHoudt releasing today.

@AdriVanHoudt
Copy link
Contributor Author

🎉

@AdriVanHoudt
Copy link
Contributor Author

@macdonst I think you missed this PR in the changelog ;)

@luiscvalmeida
Copy link

@AdriVanHoudt does forceShow option blocks the push.on('notification') event from being called?
I just commented in here: #642

@diegoherrera
Copy link

my app not Show notification when application is in foreground, could help me find the problem.

*my code in server is por example *

var registrationIds = [];
var sender = new gcm.Sender('xxxxxxxxxxxxxxxxxxxxxx');
var message = new gcm.Message();
message.addData('message',"u270C Peace, Love u2764 and PhoneGap u2706!");
message.addData('title','Push Notification Sample' );
message.addData('msgcnt','3');
message.addData('forceShow',true);
message.addData('additionalData',"{ id:1, name:'diego herrera' }");
message.addData('style', 'picture');
message.addData('picture', 'http://36.media.tumblr.com/c066cc2238103856c9ac506faa6f3bc2/tumblr_nmstmqtuo81tssmyno1_1280.jpg');
message.addData('summaryText', 'The internet is built on cat pictures');
message.timeToLive = 3000;
registrationIds.push('A...................................ccccccccccccccccc');
sender.send(message, registrationIds, 4, function (err,response) {
if(err) {
console.log("error " + err);
} else {
console.log(response);
}
});

*and my code in aplicattion is *

var push = PushNotification.init({
"android": {
"senderID": "xxxxxxxxxxx",
"forceShow": "true"
},
"ios": {"alert": "true", "badge": "true", "sound": "true"},
"windows": {}
});

     push.on('registration', function(data) {
        console.log("registration event");           
        console.log(JSON.stringify(data));
        app.gcm = data.registrationId; 
    });

     push.on('notification', function(data) {
        console.log("notification event");
        console.log(JSON.stringify(data));     
    });

     push.on('error', function(e) {
        console.log("push error");
    });

@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.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Show notification even application is in foreground
6 participants