When device screen off then how to handle firebase

2020-03-31 10:06发布

I will try to handle firebase notification when device screen off means wake up the device when a notification arrives. In my project notification divide into so may state so how I can wake up device programmatically whenever required. May also use wake lock, but when screen off then actually

    @Override
    public void onMessageReceived(RemoteMessage message) {
    }

This method not called so in this circumstances so how I can wake up the device? Can any buddy help me with this issue?

1条回答
时光不老,我们不散
2楼-- · 2020-03-31 10:41

There are two types of messages in FCM (Firebase Cloud Messaging):

Notification Messages: These messages trigger the onMessageReceived() callback only when your app is in foreground

Data Messages: Theses messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed

NOTE: Be sure you're not adding JSON key notification

The following message will not call your onMessageReceived() when your app is in the background or killed, and you can't customize your notification.

{
      "to": "example",
      "notification": {
        "title" : "title",
        "text": "text"
       }
    }

but instead using this will work

{
       "to": "example",
       "data": {
           "text":"text",
           "title":"title"
       }
    } 
查看更多
登录 后发表回答