Push notifications aren't received on Android using Unity 3D

0 votes

Hello, I'm trying to integrate your push notification service into an existing game project, but I'm running into difficult regarding push notifications:

 

I built and tested the Push test Unity project using my own Google project ID, user ID, API key, etc, and it worked successfully.  But when I make all the same API calls in my own project, I see that the push notification was successfully sent, but it does not get received on the other end (on the Android device).

 

As far as I can tell, everything substantial is the same between the two projects except for:

 

1) line 32 of the AndroidManifest.xml, where the name is different (your sample uses com.shephertz.app42.unity.android.test.MainActivity, while this will crash my game on launch, so I use "com.unity3d.player.UnityPlayerNativeActivity").

 

2) Likewise, I use com.unity3d.player.UnityPlayerNativeActivity for the "onMessageOpen" meta data inside the "service" tag.

 

3) Instead of calling all the APIs in Start() as is done in the sample, I make the following calls in Start():

 

sp = new ServiceAPI (apiKey, secretKey); // For JSON data storage

App42API.Initialize(apiKey, secretKey);

 

And then call App42.SetLoggedInUser() and RegisterForPush() after the user has manually entered their user name to log in.  After all that is done, I make the call to sendPushToUser().

 

My guess is that the problem lies either in the aforementioned differences in the AndroidManifest.xml, or in the fact that one or more of the .jar files under Plugins/Android (which were copied from the Push sample project, as instructed in the documentation) contains code or a structure that is specific to the Push sample project, and not immediately re-usable in a different project with a different bundle identifier.  For example the "sampleAndroid.jar" file appears to be structured in a manner (containing the "MainActivity" class) that is specific to the push test project.

 

Here is a link to my AndroidManifest.xml:

http://codeshare.io/5MIMs

Do you have any ideas I can try to get push notifications working in my own project?  Thanks!

asked Jun 3, 2014 in App42PaaS & BPaaS by Brady Wright (35 points)

1 Answer

0 votes
 
Best answer
Can you check Push Logs inside AppHQ console under Push notification tab to see what is the root cause of this error?
answered Jun 3, 2014 by ajay123 (899 points)
selected Jun 6, 2014 by Brady Wright
I just have 15 entires with "Module" as "App42_PushNotification", "Type" as "INFO", and "Message" as:

Message Sent to  device: APA91bGIHv04F1qJ62IWAPI3g2bmgS9g6444PVdlApj0GLrUz_WgLUISP62FjPndTxmJPTG-o5NRke1x3OtKQtf5vu2jZ0Ch4epdDuTGdLWBKsO8qtCzd80OIINa5BaeygFKtkXfCLR_deWiu2iXf2I4z_Ee6UmWZDwZxQyPxU2HcRPRqi6A0AQsrmJC98tG6jD56cRIrBe2 : [ messageId=0:1401825650714541%acaca7f9f9fd7ecd ]

I don't see anything that looks like an error.  As I said, I think the problem is just that the app on the device isn't receiving the push somehow.  I suspect it is either the AndroidManifest.xml, or one of the .jar files that hasn't been modified from those provided in the Unity 3D push example project (and no instructions were given on how to modify them to make them work, aside from the mentioned UI customization which I didn't take to be necessary).
Means your server side configuration is perfect. Your AndroidManifest looks good too. The only thing you might be missing is your Build Identifier setting in Unity Editor. Make sure it is set to com.AnBSoft.AsynchTest
It is set to that.  Everything worked perfectly in the sample project using my Google project ID, etc.  I cannot figure out what is wrong with my own project.  Any other ideas what might be wrong?  What about the sampleAndroid.jar?  What is it for, why do I need it?  If I need it, then doesn't it need to be rebuilt differently since, after extracting its contents, I see the MainActivity.class file is located under com/shephertz/app42/unity/android/test, which corresponds to the bundle identifier of the sample project.
You can remove that jar cause you have your own activity. It would be great if you can share console log and see the error when message arrives. I am sure we will get the clue from there.
Thanks
Ajay (App42 Platform)
Filtering logcat for "shephertz", I received the following lines when calling SendPushMessageToUser():

06-05 12:34:05.420: I/Unity(4511): com.shephertz.app42.paas.sdk.csharp.connection.RESTConnectorWWW:buildResponseForObjectService(String, App42CallBack, String)

06-05 12:34:05.420: I/Unity(4511): com.shephertz.app42.paas.sdk.csharp.connection.<execute>d__3:MoveNext()

06-05 12:34:05.420: I/Unity(4511): com.shephertz.app42.paas.sdk.csharp.connection.<WaitForRequest>d__0:MoveNext()

06-05 12:34:08.083: W/GTalkService(4437): [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.shephertz.app42.unity.android.test (has extras) }


Notice that the last line is a warning and contains the bundle identifier used in the sample project, instead of "com.AnBSoft.AsynchTest" which is the bundle identifier of my project.  But as you can see, the old sample bundle identifier doesn't appear anywhere in my AndroidManifest.xml, nor in Player settings, nor in any of my scripts.  So where is this old sample bundle ID coming from?  Could it be in one of the other .jar files copied from the sample project?  I removed sampleAndroid.jar, but here are the files still in my project that I copied from the sample (per the tutorial's instructions):

android-support-v4.jar
app42pushservice.jar
gcm.jar

Do any of these files need to be removed or customized to my specific project?  I feel we are getting close to finding the culprit!  Thanks!
All your copied files are fine. It is just a setting that is a culprit here and most probably is it bundle identifier. Can you check the console log at the time of when message gets received on the device. Try sending message from AppHQ console and see what is there in the log.
If you need to customize of Push notification then only rebuild from source is required otherwise it will work as it is.
It is not my bundle identifier. As I have already said, it is correctly set.  This is an entirely separate project from the Android sample.  The only way something from the Android sample project could have made it into this one is from the files that I copied from it (the files under Plugins/Android, which the tutorial said to copy).  You can see here that it is correctly set:

http://snag.gy/RXIVS.jpg

Nothing appears in the console when a message is received.  Just logcat, and only the warning I pasted earlier. I tried sending a push from AppHQ and got the same warning in logcat and nothing in the console:

06-05 14:41:52.922: W/GTalkService(4437): [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.shephertz.app42.unity.android.test (has extras) }

The push log in AppHQ also shows:
Message Sent to  device: APA91bGIHv04F1qJ62IWAPI3g2bmgS9g6444PVdlApj0GLrUz_WgLUISP62FjPndTxmJPTG-o5NRke1x3OtKQtf5vu2jZ0Ch4epdDuTGdLWBKsO8qtCzd80OIINa5BaeygFKtkXfCLR_deWiu2iXf2I4z_Ee6UmWZDwZxQyPxU2HcRPRqi6A0AQsrmJC98tG6jD56cRIrBe2 : [ messageId=0:1401997309330721%acaca7f9f9fd7ecd ]

But I think I figured out where the "com.shephertz.app42.unity.android.test" is coming from.  I had installed the example project on my test device, and I had built that project using the same API and secret keys, as well as Google project ID as the project I am testing now.  So the same IDs were being used, and that example app was still installed.  So my device was receiving the push notification and saw that it was meant for the example project (since that was the first one installed that registered itself using those IDs).  So the example app was trying to process the notifications.

So I uninstalled the example app and re-installed my test app.  The first push I sent generated an error in the log that the application that was registered for the push notification could not be found (because I uninstalled it).  On subsequent tries, nothing happened in the log at all.  So now I send push notifications (either from AppHQ or from another device) and nothing appears in the log at all.

Shouldn't re-installing and re-running my test app cause all the associated IDs to be re-registered with the device, allowing it to pick up push notifications the way the push sample app was?  Or is it that once I have installed an app using those IDs, if I want to move to a new project, I need to throw all those IDs away and use entirely new ones?
I have some new information:

I tried removing my only registered user from the "Users" list under "Push Notification" in AppHQ.  I now have no users under that list, so no devices are registered there.  So I re-ran my app which logs the user in, and then calls RegisterForPush(), and all the other usual push-related API calls.  But the list of registered users/devices is still empty!  So I think the only reason I had a user registered with my device before is because the Push sample app succeeded in registering.  But my app does not for some reason.

So I monitored logcat while installing the app, and noticed this:

06-05 18:40:25.647: W/PackageManager(750): Not granting permission android.permission.HARDWARE_TEST to package com.AnBSoft.AsynchTest (protectionLevel=2 flags=0x8be46)

Then I put Debug.Log()s in RegisterForPush() as well as in StoreDeviceId() to see if perhaps somehow these are getting run.  Both are getting called at runtime according to the logs, so it looks like everything is running, but for whatever reason, my app's attempt to register the device and user with App42 isn't working.  No errors are thrown or logged, but the list in AppHQ is still empty.

Please help!
Okay, the issue appears to have been resolved.  The problem seems to have been with incorrect API documentation.  The docs say that only the first three parameters are required parameters to StoreDeviceToken().  The fourth parameter is of type App42Callback, but it turns out, the call will fail unless you pass a valid App42Callback reference in the fourth parameter.  This is why the device was failing to be registered.  Once the device was correctly registered, everything worked.
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...