java—在前台和后台显示提示通知时出现问题

5fjcxozz  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(385)

我有一个react原生应用程序,我打算在收到消息后显示通知。对于这个perpuse,我使用了nativemodule。我想在后台和前台显示弹出通知。不幸的是,通知只显示在状态栏和通知抽屉上。基于有提示通知的文档,我应该使用fullscreenintent,但是通过添加fullscreenintent,每次通知到来并且应用程序处于前台时,不仅应用程序重新启动,而且我看不到弹出的通知。
这是我模块中的代码

public void scheduleNotification(Notification notification) {
      Intent notificationIntent = new Intent(mContext, NotificationReceiver.class);
      notificationIntent.putExtra(NotificationReceiver.NOTIFICATION_ID, 1);
      notificationIntent.putExtra(NotificationReceiver.NOTIFICATION, notification);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      long futureInMillis = SystemClock.elapsedRealtime();
      AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(mContext.ALARM_SERVICE);
      assert alarmManager != null;
      alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
  }

  public Notification getNotification(String shenaseGhabz, String shenasePardakht) {
      String notificationBody = mContext.getResources().getString(R.string.bill_payment);
      Intent intent = new Intent(mContext, MainActivity.class);
      intent.putExtra("SHENASE_GHABZ", shenaseGhabz);
      intent.putExtra("SHENASE_PARDAKHT", shenasePardakht);
      TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
      stackBuilder.addNextIntentWithParentStack(intent);
      PendingIntent pendingIntent =
              stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
      PendingIntent fullScreenIntent = PendingIntent.getActivity(getCurrentActivity(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
      NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, default_notification_channel_id);
      builder.setContentTitle(mContext.getResources().getString(R.string.app_name));
      builder.setContentText(notificationBody);
      builder.setSmallIcon(R.drawable.logo);
      builder.setAutoCancel(true);
      builder.setChannelId(NOTIFICATION_CHANNEL_ID);
      builder.setColor(mContext.getResources().getColor(R.color.light_orange));
      builder.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody));
      builder.addAction(0, mContext.getResources().getString(R.string.pay), pendingIntent);
      builder.setVisibility(Notification.VISIBILITY_PUBLIC);
      builder.setPriority(Notification.PRIORITY_MAX);
      builder.setDefaults(Notification.DEFAULT_ALL);
      builder.setFullScreenIntent(fullScreenIntent, true);
      return builder.build();
  }

这是我的接收器

public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context. NOTIFICATION_SERVICE ) ;
        Notification notification = intent.getParcelableExtra( NOTIFICATION ) ;
        if (android.os.Build.VERSION. SDK_INT >= android.os.Build.VERSION_CODES. O ) {
            int importance = NotificationManager. IMPORTANCE_HIGH ;
            NotificationChannel notificationChannel = new NotificationChannel( NOTIFICATION_CHANNEL_ID , "NOTIFICATION_CHANNEL_NAME" ,importance) ;
            assert notificationManager != null;
            notificationManager.createNotificationChannel(notificationChannel) ;
        }
        int id = intent.getIntExtra( NOTIFICATION_ID , 0 ) ;
        assert notificationManager != null;
        notificationManager.notify(id , notification) ;
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题