| | |
| | | public void onMessageReceived(RemoteMessage remoteMessage) { |
| | | Log.d(TAG, "在前台还是在后" + isAppInForeground(this)); |
| | | Log.d(TAG, "来自RemoteMessage: " + JSON.toJSONString(remoteMessage)); |
| | | Log.d(TAG, "来自: " + remoteMessage.getFrom()); |
| | | |
| | | |
| | | // // 数据消息:任何状态都会执行 |
| | | // if (!remoteMessage.getData().isEmpty()) { |
| | | // handleDataMessage(remoteMessage.getData()); // 总是执行 |
| | |
| | | // if (remoteMessage.getNotification() != null) { |
| | | // handleNotification(remoteMessage.getNotification()); // 只有前台执行 |
| | | // } |
| | | |
| | | String title = "";//推送标题 |
| | | String content = "";//推送内容 |
| | | if (remoteMessage.getNotification() != null) { |
| | | title = remoteMessage.getNotification().getTitle(); |
| | | content = remoteMessage.getNotification().getBody(); |
| | | } |
| | | if (isAppInForeground(this)) { |
| | | // 检查消息是否包含数据负载 |
| | | if (!remoteMessage.getData().isEmpty()) { |
| | | PushMessageInfoBean pushMessageInfoBean = HdlPushLogic.getInstance().pushDataProcessing("", "", JSON.toJSONString(remoteMessage.getData())); |
| | | PushMessageInfoBean pushMessageInfoBean = HdlPushLogic.getInstance().pushDataProcessing(title, content, JSON.toJSONString(remoteMessage.getData())); |
| | | HdlPushLogic.getInstance().PushPushCommonData(this, pushMessageInfoBean, false); |
| | | // Log.d(TAG, "消息数据: " + remoteMessage.getData()); |
| | | // handleDataMessage(remoteMessage.getData()); |
| | | } |
| | | } else { |
| | | // 检查消息是否包含通知负载 |
| | | if (!remoteMessage.getData().isEmpty()) { |
| | | PushMessageInfoBean pushMessageInfoBean = HdlPushLogic.getInstance().pushDataProcessing("", "", JSON.toJSONString(remoteMessage.getData())); |
| | | PushMessageInfoBean pushMessageInfoBean = HdlPushLogic.getInstance().pushDataProcessing(title, content, JSON.toJSONString(remoteMessage.getData())); |
| | | this.showCustomNotification(pushMessageInfoBean); |
| | | } |
| | | } |
| | |
| | | createNotificationChannel(notificationManager); |
| | | } |
| | | |
| | | // 构建通知内容 |
| | | NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "fcm_channel") |
| | | .setSmallIcon(R.drawable.desktop_logo) |
| | | .setSmallIcon(R.drawable.notification_logo) |
| | | .setContentTitle(pushMessageInfoBean.getTitle()) |
| | | .setContentText(pushMessageInfoBean.getContent()) |
| | | .setAutoCancel(true) |
| | | .setWhen(System.currentTimeMillis()); |
| | | |
| | | // 设置点击意图 |
| | | // 设置点击意图,为通知添加点击后的跳转意图 |
| | | Intent intent = createNotificationIntent(pushMessageInfoBean.getExpandData()); |
| | | PendingIntent pendingIntent = PendingIntent.getActivity( |
| | | this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); |
| | | builder.setContentIntent(pendingIntent); |
| | | |
| | | // 显示通知 |
| | | notificationManager.notify((int) System.currentTimeMillis(), builder.build()); |
| | | // 发送通知,通知栏显示通知 |
| | | int notificationId = (int) System.currentTimeMillis(); // 每个通知的唯一ID |
| | | notificationManager.notify(notificationId, builder.build()); |
| | | } |
| | | |
| | | @RequiresApi(api = Build.VERSION_CODES.O) |
| | | private void createNotificationChannel(NotificationManager notificationManager) { |
| | | NotificationChannel channel = new NotificationChannel( |
| | | "fcm_channel", |
| | | "FCM 消息", |
| | | "FCM Message", |
| | | NotificationManager.IMPORTANCE_HIGH |
| | | ); |
| | | channel.setDescription("Firebase Cloud Messaging 通知"); |
| | | channel.setDescription("Firebase Cloud Messaging Notification"); |
| | | notificationManager.createNotificationChannel(channel); |
| | | } |
| | | |
| | |
| | | if (TextUtils.isEmpty(token)) { |
| | | return; |
| | | } |
| | | UserConfigManage.getInstance().setPushToken(token); |
| | | UserConfigManage.getInstance().Save(); |
| | | |
| | | BaseEventBus bus = new BaseEventBus(); |
| | | bus.setTopic(ConstantManage.refresh_push_token); |
| | | bus.setType(token); |