代码之家  ›  专栏  ›  技术社区  ›  Alok Singh

当应用程序位于前台时,firebase推送通知中的点击重定向出现问题?

  •  0
  • Alok Singh  · 技术社区  · 6 年前

    我刚刚实现了firebase推送通知,并在应用程序处于后台和前台时,在单击通知时实现了重定向。只要检查我的代码,请给我解决方案。如果您遇到任何代码问题,请向我解释。单击通知仅在应用程序位于后台时有效当我的应用程序位于前台时不起作用在这两种情况下都会创建通知在前台情况下不会发生唯一重定向。

      public class MyFirebaseMessagingService extends FirebaseMessagingService {
                private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
                NotificationCompat.Builder builder;
                String CHANNEL_ID = "my_channel_01";
                PendingIntent pendingIntent;
                private NotificationUtils notificationUtils;
                private NotificationManager mNotificationManager;
    
                private static int getNotificationIcon() {
                    return R.mipmap.ic_launcher;
                }
    
                @Override
                public void onMessageReceived(RemoteMessage remoteMessage) {
                    Log.e(TAG, "From: " + remoteMessage.getFrom());
                    Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
    
                    if (remoteMessage.getNotification() == null)
                        return;
                    // Check if message contains a notification payload.
                    if(remoteMessage.getNotification() != null) {
                        handleNotification(remoteMessage, remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle());
                    }
    
                }
    
                private void handleNotification(RemoteMessage remoteMessage, String body, String title) {
                    if (mNotificationManager == null) {
                        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
                    }
    
                    boolean isNotificationSound = true;
                    //TODO: update count if app is open.
                    if (!NotificationUtils.isAppIsInBackground(this)) {
                        if (isNotificationSound) {
                            try {
                                Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                                Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
                                r.play();
                            } catch (Exception e) {
                            }
                        }
                        covertRemoteToJson(remoteMessage, body, title);
                    }else {
                        covertRemoteToJson(remoteMessage, body, title);
                    }
                }
    
                private void covertRemoteToJson(RemoteMessage remoteMessage, String body, String title) {
                    if (remoteMessage.getData().size() > 0) {
                        Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
    
                        try {
                            //Map<String, String> params = remoteMessage.getData();
                            if (!StringUtils.isNullOrEmpty(remoteMessage.getData().toString())) {
                                JSONObject json = new JSONObject(remoteMessage.getData().toString());
                                handleDataMessage(json, body, title);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            e.getMessage();
                            Log.e(TAG, "Exception: " + e.getMessage());
                        }
                    }
                }
    
                private void handleDataMessage(JSONObject json, String body, String title) {
                    int requestID = (int) System.currentTimeMillis();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        int importance = NotificationManager.IMPORTANCE_HIGH;
                        assert mNotificationManager != null;
                        NotificationChannel mChannel = mNotificationManager.getNotificationChannel(CHANNEL_ID);
                        if (mChannel == null) {
                            mChannel = new NotificationChannel(CHANNEL_ID, title, importance);
                            mChannel.enableVibration(true);
                            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                            mNotificationManager.createNotificationChannel(mChannel);
                        }
                        builder = new NotificationCompat.Builder(this);
                        Intent intent = new Intent(this, MainActivity.class);
                        intent.setAction(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_LAUNCHER);
                        intent.putExtra(AppConstants.EXTRAS.IS_FROM_NOTIFICATION, true);
                        intent.putExtra(AppConstants.EXTRAS.NOTIFICATION_LANDING_DATA, json.toString());
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
                        builder.setContentTitle(title)                            // required
                                .setSmallIcon(getNotificationIcon())   // required
                                .setContentText(body).setWhen(System.currentTimeMillis()) // required
                                .setDefaults(Notification.DEFAULT_ALL)
                                .setAutoCancel(true)
                                .setContentIntent(pendingIntent)
                                .setChannelId(CHANNEL_ID)
                                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                    } else {
                        builder = new NotificationCompat.Builder(this);
                        Intent intent = new Intent(this, MainActivity.class);
                        intent.setAction(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_LAUNCHER);
                        intent.putExtra(AppConstants.EXTRAS.IS_FROM_NOTIFICATION, true);
                        intent.putExtra(AppConstants.EXTRAS.NOTIFICATION_LANDING_DATA, json.toString());
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
                        builder.setContentTitle(title)                            // required
                                .setSmallIcon(getNotificationIcon())   // required
                                .setContentText(body).setWhen(System.currentTimeMillis()) // required
                                .setDefaults(Notification.DEFAULT_ALL)
                                .setAutoCancel(true)
                                .setChannelId(CHANNEL_ID)
                                .setContentIntent(pendingIntent)
                                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                                .setPriority(Notification.PRIORITY_HIGH);
                    }
                    Notification notification = builder.build();
                    mNotificationManager.notify(requestID, notification);
                }
    
    
            }
    
    0 回复  |  直到 6 年前