Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何创建服务FirebaseMessagingService始终在后台处于活动状态_Android_Firebase_Firebase Cloud Messaging - Fatal编程技术网

Android 如何创建服务FirebaseMessagingService始终在后台处于活动状态

Android 如何创建服务FirebaseMessagingService始终在后台处于活动状态,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,大家好,你们能帮我创建FirebaseMessagingService始终在后台活动(如果有推送通知),然后为唤醒服务和睡眠应用程序添加5分钟的时间吗 多谢各位 这个我的代码 public class FcmServiceMessage extends FirebaseMessagingService{ String message, title; @Override public void onCreate() { // TODO Auto-generated method stub

大家好,你们能帮我创建
FirebaseMessagingService
始终在后台活动(如果有
推送通知
),然后为唤醒服务和睡眠应用程序添加5分钟的时间吗

多谢各位

这个我的代码

public class FcmServiceMessage extends FirebaseMessagingService{

String message, title;
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
}

@Override
public void onMessageReceived(RemoteMessage mMessage) {
    //Bundle extras = data.getExtras();

    Map data =  mMessage.getData();
    //String message = data.get("message").toString();
    if (data.containsKey("message")){
        message = data.get("message").toString();
    }

    if (data.containsKey("title")){
        title = data.get("title").toString();
    }

    showNotification();

}

private void showNotification(){
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"channel_Id")
            .setOngoing(false)
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.notification_icon_small)
            .setColor(getResources().getColor(R.color.white))
            .setLargeIcon(convertToBitmap(getApplication().getResources().getDrawable(R.mipmap.ic_launcher)))
            .setContentTitle(title)
            .setContentText(message)
            .setTicker(message)
            .setStyle(bigTextStyle)
            .setPriority(Notification.PRIORITY_HIGH);

    return builder.build();

    notificationChannel = new NotificationChannel("channel_Id", "MyApps", NotificationManager.IMPORTANCE_HIGH);
    notificationChannel.setLightColor(R.color.white);
    notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.createNotificationChannel(notificationChannel);
 }
}
然后

实现'com.google.firebase:firebase消息:17.1.0'

当应用程序位于后台时,不会调用onMessageReceived()。几个月前我有这个问题。我通过override handlerIntent方法解决了这个问题。但您应该将firebase消息传递库降级为实现“com.google.firebase:firebase消息传递:10.2.1”

之后:

@Override
    public void handleIntent(Intent intent) {
        try
        {
            if (intent.getExtras() != null)
            {
                RemoteMessage.Builder builder = new RemoteMessage.Builder("MessagingService");

                for (String key : intent.getExtras().keySet())
                {
                    builder.addData(key, intent.getExtras().get(key).toString());
                }

                onMessageReceived(builder.build());
            }
            else
            {
                super.handleIntent(intent);
            }
        }
        catch (Exception e)
        {
            super.handleIntent(intent);
        }
    }
如果您不想降级库,那么

您可以指定单击操作,以指示用户点击通知时应启动的意图。如果未指定单击操作,则使用主活动

启动意图后,您可以使用

getIntent().getExtras();
检索包含随通知消息一起发送的任何数据的集合


有关通知消息的详细信息,请参见

消息服务应已在后台创建。这听起来像是一个错误。你有什么问题?那是什么呢?你能给我一些代码吗?比如@Frankvanpoffelen,因为我遵循Firebase教程,但在我的手机上,华硕、索尼和三星都没有推送通知。我想我需要后台活动或应用程序的服务消息来交换这不是堆栈溢出的工作方式。如果您很难让FCM正常工作,请共享(请阅读链接,它非常有用)。@FrankvanPuffelen好的,我已经添加了FCM,但一小时后没有收到推送通知。应用程序需要再次打开,然后才能在后台获得通知,但在一小时后再次消失,没有推送,我将此应用程序放在主活动或服务上?在您的FcmServiceMessage类上