Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.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
未调用FCM android onmessagereceived_Android_Firebase_Push Notification_Firebase Cloud Messaging - Fatal编程技术网

未调用FCM android onmessagereceived

未调用FCM android onmessagereceived,android,firebase,push-notification,firebase-cloud-messaging,Android,Firebase,Push Notification,Firebase Cloud Messaging,我已经在应用程序中实现了FCM。其他firebase功能工作正常,即上传到firebase存储和firebase实时消息。但是,当我第一次向设备发送推送通知时,它显示已成功发送通知,但未调用messagereceived。然后,当我发送另一个推送通知时,它立即显示未注册。而且之后总是没有注册 public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMes

我已经在应用程序中实现了FCM。其他firebase功能工作正常,即上传到firebase存储和firebase实时消息。但是,当我第一次向设备发送推送通知时,它显示已成功发送通知,但未调用messagereceived。然后,当我发送另一个推送通知时,它立即显示未注册。而且之后总是没有注册

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d("myLog", "From: " + remoteMessage.getFrom());
    Log.d("myLog", "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
}
app.gradle:

apply plugin: 'com.google.gms.google-services'
project.gralde:

classpath 'com.google.gms:google-services:3.1.0'
舱单:

<service android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
</service>


设备令牌被正确刷新,当我清除数据并重新打开应用程序时,它会立即打印新的设备令牌

您使用什么设备来测试此功能?众所周知,小米和乐视等一些制造商阻止应用程序接收FCM通知

对于三星、摩托罗拉等设备:确保您处理的信息类型正确。您可以发送两种类型的FCM消息

  • 通知消息
  • 数据信息
  • 阅读本页了解更多信息


    这取决于来自控制台的消息类型。 如果只有通知消息,则如果您的应用程序已关闭,则不会触发接收器。 如果它包含通知消息数据消息,那么当应用程序位于前台时,您的应用程序仍然可以处理这些消息

    确保您正在使用此依赖项
    编译'com.google.firebase:firebase messaging:10.2.1'

    onMessageReceived(RemoteMessage RemoteMessage)方法,该方法基于以下情况调用

    • FCM响应带有通知数据块:
    {   “”:“设备令牌列表”, “通知””:{ “正文”:“您的通知正文”, “标题”:“通知的标题”   }, “数据””:{ “正文”:“数据中的通知正文”, “标题”:“标题中的通知标题”, “密钥1”:“密钥1的值”, “图像url”:“www.abc.com/xyz.jpeg”, “键2”:“键2的值”   } }

  • 前台应用程序:
  • onMessageReceived(RemoteMessage RemoteMessage)调用,在通知栏中显示LargeIcon和BigPicture。我们可以从通知数据块读取内容

  • 后台应用程序:
  • onMessageReceived(RemoteMessage RemoteMessage)未调用,系统托盘将从通知块接收消息并读取正文和标题,并在通知栏中显示默认消息和标题

    • FCM响应仅包含数据块:
    在这种情况下,从json中删除notobocation

    {   “”:“设备令牌列表”, “数据””:{ “正文”:“数据中的通知正文”, “标题”:“标题中的通知标题”, “密钥1”:“密钥1的值”, “图像url”:“www.abc.com/xyz.jpeg”, “键2”:“键2的值”   } }

  • 前台应用程序:
  • onMessageReceived(RemoteMessage RemoteMessage)调用,在通知栏中显示LargeIcon和BigPicture。我们可以从通知数据块读取内容

  • 后台应用程序:
  • onMessageReceived(RemoteMessage RemoteMessage)调用时,系统托盘将不会收到消息,因为通知键不在响应中。在通知栏中显示LargeIcon和BigPicture

    代码

     private void sendNotification(Bitmap bitmap,  String title, String 
        message, PendingIntent resultPendingIntent) {
    
        NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
        style.bigPicture(bitmap);
    
        Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    
        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = mContext.getString(R.string.default_notification_channel_id);
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "channel_name", NotificationManager.IMPORTANCE_HIGH);
    
            notificationManager.createNotificationChannel(notificationChannel);
        }
        Bitmap iconLarge = BitmapFactory.decodeResource(mContext.getResources(),
                R.drawable.mdmlogo);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.drawable.mdmlogo)
                .setContentTitle(title)
                .setAutoCancel(true)
                .setSound(defaultSound)
                .setContentText(message)
                .setContentIntent(resultPendingIntent)
                .setStyle(style)
                .setLargeIcon(iconLarge)
                .setWhen(System.currentTimeMillis())
                .setPriority(Notification.PRIORITY_MAX)
                .setChannelId(NOTIFICATION_CHANNEL_ID);
    
    
        notificationManager.notify(1, notificationBuilder.build());
    
    
    }
    
    参考链接:


    我已经检查了大多数设备,如moto X play、三星s6等。更新了我的答案。显示您的设备注册码