Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.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 无论活动是否在后台,Firebase通知都不起作用_Android_Firebase_Push Notification_Firebase Cloud Messaging - Fatal编程技术网

Android 无论活动是否在后台,Firebase通知都不起作用

Android 无论活动是否在后台,Firebase通知都不起作用,android,firebase,push-notification,firebase-cloud-messaging,Android,Firebase,Push Notification,Firebase Cloud Messaging,我的代码与firebase示例代码提供的代码相同。当活动处于前台状态或打开时,它工作正常。但当活动关闭或处于后台状态时,它将无法正常工作。问题是这样的 未显示应用程序的应用程序图标 默认情况下,将应用程序名称作为通知的标题 当应用程序处于后台状态或未在设备上打开时,会出现通知,LogCat会显示以下详细信息: 09-16 15:55:56.056 12113-12113/com.testdemofb D/FirebaseApp: com.google.firebase.auth.Firebase

我的代码与firebase示例代码提供的代码相同。当活动处于前台状态或打开时,它工作正常。但当活动关闭或处于后台状态时,它将无法正常工作。问题是这样的

  • 未显示应用程序的应用程序图标

  • 默认情况下,将应用程序名称作为通知的标题

  • 当应用程序处于后台状态或未在设备上打开时,会出现通知,LogCat会显示以下详细信息:

    09-16 15:55:56.056 12113-12113/com.testdemofb D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
    09-16 15:55:56.089 12113-12113/com.testdemofb D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
    09-16 15:55:56.176 12113-12113/com.testdemofb I/FA: App measurement is starting up, version: 9452
    09-16 15:55:56.177 12113-12113/com.testdemofb I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
    09-16 15:55:56.286 12113-12113/com.testdemofb I/FirebaseInitProvider: FirebaseApp initialization successful
    09-16 15:55:56.518 12113-12147/com.testdemofb I/FA: Tag Manager is not found and thus will not be used
    
    如果有任何解决办法,那么请帮助我

    谢谢

    MyFirebaseMessagingService.java

    package com.testdemofb;
    
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Color;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import android.support.v4.app.NotificationCompat;
    import android.util.Log;
    import android.widget.Toast;
    
    import com.google.firebase.messaging.FirebaseMessagingService;
    import com.google.firebase.messaging.RemoteMessage;
    
    public class MyFirebaseMessagingService extends FirebaseMessagingService {
    
    private static final String TAG = "MyFirebaseMsgService";
    
    
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    
        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            sendNotification(remoteMessage.getData().get("title"));
        }
    
        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            Log.d(TAG, "Message Notification BodyLocalize: " + remoteMessage.getNotification().getBodyLocalizationKey());
            Log.d(TAG, "Message Notification Title: " + remoteMessage.getNotification().getTitle());
            Log.d(TAG, "Message Notification TitleLocalize: " + remoteMessage.getNotification().getTitleLocalizationKey());
            sendNotification(remoteMessage.getNotification().getBody());
        }
    
    
    }
    
    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
    
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setLargeIcon(bm)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Title")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
    
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    
    }
    }
    
    AndroidManifest.xml文件放在应用程序标记之间,如下所示


    FCM使用两种类型的消息,即。通知和数据。阅读更多关于它的信息。我想你是在发通知。您需要发送一条数据消息

    像这样的

    {
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
       "data" : {
     "Nick" : "Mario",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
    },
    }
    

    FCM使用两种类型的消息,即。通知和数据。阅读更多关于它的信息。我想你是在发通知。您需要发送一条数据消息

    像这样的

    {
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
       "data" : {
     "Nick" : "Mario",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
    },
    }
    

    从Firebase控制台发送通知:适用于后台和前台应用程序

    替代onMessageReceived,重写FirebaseMessagingService的zzm(),并从此处创建自定义通知

    @Override
    public void zzm(Intent intent) {
        Log.e(TAG, "zzm : " + intent);
        createNotification();        
    }
    
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    
    }
    

    从Firebase控制台发送通知:适用于后台和前台应用程序

    替代onMessageReceived,重写FirebaseMessagingService的zzm(),并从此处创建自定义通知

    @Override
    public void zzm(Intent intent) {
        Log.e(TAG, "zzm : " + intent);
        createNotification();        
    }
    
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    
    }
    

    我认为FireBase是这样工作的。。。


    我认为FireBase是这样工作的。。。


    您是否可以共享生成Notifications的实现共享生成通知的代码@raguswaminate?您是否可以检查sendnotification方法是否被调用?尝试在那里登录并检查它在应用程序处于前台状态时被调用,但在应用程序关闭和后台状态@RaguSwaminathancall sendnotification方法超出if语句时未被调用,并检查其是否有效?您能否共享生成Notifications的实现共享生成通知@RaguSwaminathando的代码是否调用sendnotification方法?尝试在那里登录并检查它在应用程序处于前台状态但未在应用程序关闭和后台状态@RaguSwaminathancall sendnotification method的if语句之外被调用时被调用,并检查它是否有效?我不确定为什么人们会对它嗤之以鼻,但这似乎是如何工作的。我不确定为什么人们会对它嗤之以鼻,但这似乎是如何工作的你能添加更多细节吗?你能添加更多细节吗