3

I'm very experienced on Unity and iOS and I can deploy my Unity app to Android. But I don't know anything about native Android stuff.

My problem is about receiving a GCM message and showing a notification when the android app is in background. I implemented it and it works perfect when the app is in foreground but my GCM message listener isn't being triggered when the app is in background and I can't show the notification.

I use this plugin for receiving GCM messages : https://github.com/kskkbys/unity-gcm I use this for showing the notification : https://www.assetstore.unity3d.com/en/#!/content/9484

I tried to reach out the authors of these plugins but they didn't reply for a long time...

Xtro
  • 301
  • 2
  • 12
  • @Steven: Unity is the product name. Only their domain name is unity3d.com – Xtro Jul 24 '14 at 19:47
  • 1
    Sure, but tags about the Unity game engine should be asked under the [Unity3d tag](https://stackoverflow.com/tags/unity3d/info) while questions for the Microsoft Pattern & Practices Unity IoC library should be asked under the [Unity tag](https://stackoverflow.com/tags/unity/info). The tags don't lie, and I didn't create those tags. – Steven Jul 24 '14 at 20:07
  • I have to bump my question since I'm still hopeless... – Xtro Jul 25 '14 at 15:08

2 Answers2

1

I solved my problem by using "Android Native" plugin from StansAssets in asset store. With his implementation, android device shows a notification when the cloud message arrives even if the app is in the background. Tapping on the notification opens up the app.

Xtro
  • 301
  • 2
  • 12
0

You may find the concept of the event bus interesting. I'll try and show you an example, although I've never used it with notifications before. I'll use Toast messages as a substitute, since that's what I'm comfortable with.

The idea is fairly simple, in that any object (activity) can "ride the bus":

bus.register(this)

The you need subscriber methods to run when the bus "beeps"(emits an event):

public void onEvent(NotificationEvent event) {
Toast.makeText(this, 
    event.payload.getString("message"), 
    LENGTH_SHORT)
.show();
}

The, when an object needs to alight the bus:

bus.unregister(this);

Also, to actually make the bus "beep":

NotificationEvent event = new NotificationEvent(bundle);
bus.post(event);

This triggers the call to the onEvent(NotificationEvent event) method in every object that has a seat on the bus. You can add different subscriber methods (such as onEvent(LocationEvent event)) and these method will only be run when an event of the same type is emitted bus.post(new LocationEvent()) There is no limit to how many objects can register with the bus.

With a small amount of modification, I see no reason why you can't adapt this to a notification method, and trigger events based on your program.

Wolfish
  • 960
  • 2
  • 8
  • 34
  • I have to say that I didn't quite understand what you are explaining. As I stated in my question, I don't have any native android experience. This includes java, android sdk and its classes. I don't touch them at all. All I want to do is to be able to receive GCM messages when the app is background. It's extremely easy to do it on iOS. I don't understand why Android platform is so hard. – Xtro Jul 24 '14 at 19:51
  • 1
    Think of the bus as a process that is constantly running. By attaching your notification to the bus, you can ensure that it will be able to get from your application to the notification centre. Your program triggers the notification, the bus picks it up and delivers it to the user. Also, you think Android is hard? Try backend Symbian :) – Wolfish Jul 25 '14 at 15:12