如何创建多个通知?

问题描述

每个通知必须具有自己的ID。我正在向意图传递两个不同的ID。但是仅显示两个中的第二个。 我在做什么错了?

这是我的带有常量和通知功能的片段OnClick代码

public static final String NOTIFICATION_CHANNEL_ID = "10001" ;
private final static String default_notification_channel_id = "default" ;
public void onClick(View view) {
            scheduleNotification(getNotification( "10 second delay" ),10000,1 ) ;
            scheduleNotification(getNotification( "5 second delay" ),5000,2 ) ;
            OpenThis(view);
        }

private void scheduleNotification (Notification notification,int delay,int NotifID) {
    Intent notificationIntent = new Intent( this.getActivity(),Reminderbroadcast.class ) ;
    notificationIntent.putExtra(Reminderbroadcast.NOTIFICATION_ID,NotifID ) ;
    notificationIntent.putExtra(Reminderbroadcast.NOTIFICATION,notification) ;
    PendingIntent pendingIntent = PendingIntent. getbroadcast ( this.getContext(),notificationIntent,PendingIntent. FLAG_UPDATE_CURRENT ) ;
    long futureInMillis = SystemClock. elapsedRealtime () + delay ;
    AlarmManager alarmManager = (AlarmManager) this.getContext().getSystemService(Context. ALARM_SERVICE ) ;
    assert alarmManager != null;
    alarmManager.set(AlarmManager. ELAPSED_REALTIME_WAKEUP,futureInMillis,pendingIntent) ;
}

private Notification getNotification (String content) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder( this.getContext(),default_notification_channel_id ) ;
    builder.setContentTitle( "Scheduled Notification" ) ;
    builder.setContentText(content) ;
    builder.setSmallIcon(R.drawable. ic_launcher_foreground ) ;
    builder.setAutoCancel( true ) ;
    builder.setChannelId( NOTIFICATION_CHANNEL_ID ) ;
    return builder.build() ;
}

我的broadcastReceiver

public class Reminderbroadcast extends broadcastReceiver {
public static String NOTIFICATION_ID = "notification-id" ;
public static String NOTIFICATION = "notification" ;

private static final String TAG = "myLogs";
@Override
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 ) ;
    Log.d("TAG",String.valueOf(id));

    assert notificationmanager != null;
    notificationmanager.notify(id,notification) ;
}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)