Skll
Skll

Reputation: 21

Push notifications behaviour while app running in background (Parse/Unity/Android)

I've been trying Parse's integration tutorials and examples, and I can't figure out how to make the push notifications show in tray when the app is running in background.

Notifications do appear when the app is closed, though! Also, when in foreground, push notification events do trigger correctly.

Summing up, push notifications work perfectly except when running in background.

The notification shows in the log like this:

07-02 04:43:06.979: I/GCM(22111): GCM message com.parse.parseunitypushsample 0:1435804985959681%368c544ef9fd7ecd

07-02 04:43:07.033: W/GCM-DMM(22111): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.parse.parseunitypushsample (has extras) }

07-02 04:43:07.041: I/ParsePushService(16145): Push notification received. Payload: {"alert":"A test push from Parse!","push_hash":"2bf06f5e2a92eab2fcb855fc1117fa33"}

07-02 04:43:07.041: I/ParsePushService(16145): Push notification is handled while the app is foregrounded.

Notice it says "foregrounded", even when some other app is in foreground. This bothers me a bit, as if Parse decided not to show the notification because it concludes that it's on foreground. Just my guess.

I'm using:

I'm just building the tutorial project, but it needs Unity upgrade, and a couple small fixes to run. Otherwise, it's clean and seems to correctly receive push notifications, but not showing them while running on background.

Is that the expected behaviour, or am I doing something wrong?

Thank you :)

Upvotes: 2

Views: 2933

Answers (2)

Samuel Kolb
Samuel Kolb

Reputation: 21

Not sure if this helps, but I was able to work around this by editing the plugin source available here. To do this I simply cached the string value of the push msg in the ParsePushUnityHelper.java class and then retrieved it from Unity c# via Android CallStatic method. You can then use one of the publicly available Json modules to convert this from a json string to a dictionary. Keep in mind you'll have to recompile and link the plugin .jar to your Unity project. Good luck!

Upvotes: 0

DanixDani
DanixDani

Reputation: 186

If you got that, then you are almost there! I think you need to add this script to one of your GameObjects:

using UnityEngine;
using System.Collections;
using Parse;


    public class ParsePushRegistration : MonoBehaviour {
        // Use this for initialization
        void Start () {
            #if UNITY_ANDROID
            ParsePush.ParsePushNotificationReceived += (sender, args) => {                  
                AndroidJavaClass parseUnityHelper = new AndroidJavaClass ("com.parse.ParseUnityHelper");
                AndroidJavaClass unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
                AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject> ("com.unity3d.player.UnityPlayerNativeActivity");

                // Call default behavior.
                parseUnityHelper.CallStatic ("handleParsePushNotificationReceived", currentActivity, args.StringPayload);
            };              
            #endif
        }
    }

Check and keep track of this question, it might be usefull to you: Parse Unity Push Sample not working

Upvotes: 0

Related Questions