首页 文章

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

提问于
浏览
0

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

我正在使用 Firebase Cloud 消息来通知客户端某个操作 . 当应用程序被杀或处于后台时,就我正在进行调试时,我可以看到当设备收到通知时 onMessageReceived 方法没有被触发时,我有类 FCMNotificationIntentService extends FirebaseMessagingService {...} ,方法 onMessageReceived 是找到并只是一个默认构造函数,如:

public class FCMNotificationIntentService extends FirebaseMessagingService { 

   public FCMNotificationIntentService() {
        super(); 
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        sendNotification(remoteMessage);
    }
        //other methods and stuff...
}

因此,当设备收到通知时,它不会触发 onMessageReceived 而只触发构造函数,这意味着通知将在没有数据的情况下进入设备 . 为什么会发生这种情况,当应用程序处于前台时,始终会触发 onMessageReceived 并且有来自服务器的数据 .

任何建议或想法都非常感谢 .

2 回答

  • 0

    您是否覆盖onNewToken并且是否将令牌发送到服务器以允许与设备进行通信?在您的服务类中添加:

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

    你可以参考这个https://github.com/firebase/quickstart-android/issues/548

  • 0

    试试这个工作代码吧

    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;
        }
      // 
       //    }
      }
    

相关问题