How do I get a message when the app is in the background? (Android, Unity, GCM)

I am very experienced in 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 getting GCM message and showing notification when android app is running in background. I have implemented it and it works fine when the app is in the foreground, but the GCM message listener does not start when the app is in the background and I cannot show the notification.

I am using this plugin to get GCM messages: https://github.com/kskkbys/unity-gcm I am using this to show notification: https://www.assetstore.unity3d.com/en/#!/content/9484

I tried to contact the authors of these plugins, but they didn't respond for a long time ...

+2


source to share


2 answers


I solved my problem using the "Android Native" plugin from StansAssets in the asset store. With its implementation, the Android device displays a notification when a cloud message appears, even if the app is in the background. Clicking on the notification opens the application.



+1


source


You may find the event bus concept interesting. I'll try to show you an example, although I've never used it with notifications before. I will use Toast messages as replacements as I like it.

The idea is pretty simple, since any object ( activity

) can "ride the bus":

bus.register(this)

      

You need subscriber methods to trigger when the bus "beeps" (emits an event):

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

      

When an object should fail the bus:



bus.unregister(this);

      

Also, to make the bus beep:

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

      

This invokes a method call onEvent(NotificationEvent event)

on every object that takes place on the bus. You can add different subscriber methods (for example onEvent(LocationEvent event)

) and this method will only execute when an event of the same type bus.post(new LocationEvent())

is emitted. There is no limit on the number of objects that can be registered on the bus.

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

0


source







All Articles