Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.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_Firebase Cloud Messaging - Fatal编程技术网

Android Firebase通知,应用程序不会在通知单击时打开

Android Firebase通知,应用程序不会在通知单击时打开,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,我知道关于这个话题有很多问题,但我的问题有点不同 我正在使用Firebase云消息通知客户端某个操作。当应用程序被终止或处于后台并且我正在调试时,我可以看到当设备接收到通知时,onMessageReceived方法没有被触发,我就有了类 fcmnotificationtentservice扩展了FirebaseMessagingService{…},方法onMessageReceived位于该服务上,并且只是一个默认构造函数,如: public class FCMNotificationIn

我知道关于这个话题有很多问题,但我的问题有点不同

我正在使用Firebase云消息通知客户端某个操作。当应用程序被终止或处于后台并且我正在调试时,我可以看到当设备接收到通知时,onMessageReceived方法没有被触发,我就有了类
fcmnotificationtentservice扩展了FirebaseMessagingService{…}
,方法onMessageReceived位于该服务上,并且只是一个默认构造函数,如:

  public class FCMNotificationIntentService extends FirebaseMessagingService { 
   
   public FCMNotificationIntentService() {
        super(); 
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        sendNotification(remoteMessage);
    }
        //other methods and stuff...
}
因此,当设备接收到通知时,它不会触发onMessageReceived,而只触发构造函数,这意味着通知是在没有数据的情况下到达设备的。为什么会发生这种情况,当应用程序位于前台时,总是会触发onMessageReceived,并且服务器会提供数据


非常感谢您的任何建议或想法。

您是否覆盖了onNewToken,是否向服务器发送令牌以允许与设备通信?在您的服务类中添加:

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
}

您可以参考此

试试它的工作代码

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //Displaying data in log
    //It is optional
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Data: " + remoteMessage.getData());
    //        Log.d(TAG, "Notification Message Body: " + 
    remoteMessage.getNotification().getBody());
    String message_title = "";
    String from_user_id = "";
    String to_user_id = "";
    String type = "";
    String sound = "";
    String vibrate = "";
    String message = "";

    try {
        message_title = remoteMessage.getData().get("message_title");
        from_user_id = remoteMessage.getData().get("from_user_id");
        to_user_id = remoteMessage.getData().get("to_user_id");
        type = remoteMessage.getData().get("type");
        sound = remoteMessage.getData().get("sound");
        vibrate = remoteMessage.getData().get("vibrate");
        message = remoteMessage.getData().get("message");

    } catch (Exception e) {
        e.printStackTrace();
    }



    //Calling method to generate notification

  sendNotification(message_title,from_user_id,to_user_id,type,sound,vibrate,message);
  }
            boolean whiteIcon = (Build.VERSION.SDK_INT >= 
  Build.VERSION_CODES.LOLLIPOP);


//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String message_title, String from_user_id,String 
to_user_id, String type, String sound,String vibration,String message) {
 //       Intent intent = new Intent(this, MainActivity.class);

 //        if (AppData.getInstance().getReader(this)==null){
       Intent intent = new Intent(this, ChatListActivity.class);
 //        }else {
 //            intent = new Intent(this, ArticlesActivity.class);
 //            
  intent.putExtra(Const.getInstance().iFrom,Const.getInstance().notification);
 //            intent.putExtra("notification",1);
 //        }
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= 
    RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new 
    NotificationCompat.Builder(this)
           // .setSmallIcon(R.mipmap.ic_launcher)
            .setSmallIcon(getNotificationIcon())
            .setContentTitle(message_title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setDefaults(Notification.DEFAULT_ALL)
            .setPriority(Notification.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
                .setColor(whiteIcon ? getResources().getColor(R.color.colorPrimary) : 
    getResources().getColor(android.R.color.transparent));

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
    }
    private int getNotificationIcon() {
    boolean whiteIcon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
    return whiteIcon ? R.drawable.notification: R.mipmap.ic_launcher;
    }
  // 
   //    }
  }

@TaulantLoshi,您确信您的主机收到此令牌并在发送推送时正确管理它?在studio中,如果您转到工具->firebase->云消息->设置云消息,是否会显示您已连接到firebase,并且FCM已在您的依赖项中正确设置?是的,我已经这样做了,我尝试了两种不同的方法,但仍然无法触发onMessageReceived方法。