Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

android - Firebase push notifications update DB

I'm trying to send some data to my users via the new Firebase push notifications service.

I'm adding some "key-value" custom data items with 'Message text' - "dbupdate" to detect if it's an update message or a normal push notification that I want to show my users.

In my receiver I check i there is custom data and run the update, if true. If this isn't the case, I show a normal notification with the text message.

It works only when my application opened. If the application is closed, the notification is shown anyway, and my if check doesn't even running,

some code:

public class PushMessageService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Map<String,String> data = remoteMessage.getData();
        String link = data.get("link");
        String name = data.get("name");
        if (link!=null) {
            Log.d("link",link);
            Log.d("name",name);
            UpdateHelper.Go(this, link, name);
        }
        else {
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_shop_white_24dp)
                            .setAutoCancel(true)
                    .setContentTitle(remoteMessage.getNotification().getTitle())
                   .setContentText(remoteMessage.getNotification().getBody());
            NotificationManager mNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(5, mBuilder.build());
        }
    }
}

example notficiation here

Why is that so? What can I do to run my code even when the application closed?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...