有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java本地通知未在所有设备中触发

我每天在某个特定的时间都有一个本地通知要重复。我试着用一些设备进行测试,但只在一些设备上起作用。请检查下面的代码,并给出一些提示。我试过使用三星和LG设备

下面是我的闹钟接收器

 public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    MediaPlayer mediaPlayer = MediaPlayer.create(context, Settings.System.DEFAULT_NOTIFICATION_URI);
    mediaPlayer.start();


    NotificationManager manager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.logo)
            //example for large icon
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
            .setContentTitle("Hey")
            .setContentText("Did you read today")
            .setOngoing(false)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setAutoCancel(true);
    Intent i = new Intent(context, VerseActivity.class);
    PendingIntent pendingIntent =
            PendingIntent.getActivity(
                    context,
                    0,
                    i,
                    PendingIntent.FLAG_ONE_SHOT
            );
    // example for blinking LED
    builder.setLights(0xFFb71c1c, 1000, 2000);
    builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
    builder.setContentIntent(pendingIntent);
    manager.notify(12345, builder.build());
}
}

我的主要活动是什么

 public class MainActivity extends AppCompatActivity {
private final int SPLASH_DISPLAY_LENGTH = 1000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    //Setting time of the day (8am here) when notification will be sent every day (default)
    calendar.set(Calendar.HOUR_OF_DAY,15);
    calendar.set(Calendar.MINUTE, 50);
    SetAlarm(calendar.getTimeInMillis());

    //alarm service

}

private void SetAlarm(long timeInMillis) {
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.setRepeating(AlarmManager.RTC, timeInMillis, AlarmManager.INTERVAL_DAY, broadcast);
}


}

舱单。xml

    <receiver 安卓:name=".AlarmReceiver"
        安卓:enabled="true"
        安卓:exported="true"
        />

共 (2) 个答案

  1. # 1 楼答案

    在调用manager.notify(12345, builder.build());之前创建通知通道

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        manager.createNotificationChannel(channel);
    }
    builder.setChannelId(CHANNEL_ID);
    manager.notify(12345, builder.build());
    
  2. # 2 楼答案

    只是一个片段,您必须涵盖所有这些构建版本

      if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
                if (LOG_DEBUG) Log.v(TAG, " : version : <=M ");
    
            //noinspection deprecation
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    .setContentTitle(context.getString(R.string.notification_title))
                    .setContentText(context.getString(R.string.notification_subtext))
                    .setSmallIcon(R.drawable.ic_notification)
                    .setAutoCancel(true)
                    .setLights(Color.CYAN, 500, 1200)
                    .setPriority(Notification.PRIORITY_MAX)
                    .setDeleteIntent(piDismiss)
                    //.setContentIntent(pIpanel)
                    .setStyle(new NotificationCompat.DecoratedCustomViewStyle());
    
            notification = builder.build();
    
            notification.contentView = collapsedView;
            notification.bigContentView = expandedView;
    
        } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N | Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
            if (LOG_DEBUG) Log.v(TAG, " : version : N| N1 - 24: 25 ");
    
            //noinspection deprecation
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    .setContentTitle(context.getString(R.string.notification_title))
                    .setContentText(context.getString(R.string.notification_subtext))
                    .setSmallIcon(R.drawable.ic_notification)
                    .setAutoCancel(true)
                    .setLights(Color.CYAN, 500, 1200)
                    .setPriority(Notification.PRIORITY_MAX)
                    .setDeleteIntent(piDismiss)
                    //.setContentIntent(pIpanel)
                    .setCustomContentView(collapsedView)
                    .setCustomBigContentView(expandedView)
                    .setStyle(new NotificationCompat.DecoratedCustomViewStyle());
    
            notification = builder.build();
    
    
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (LOG_DEBUG) Log.v(TAG, " : version : >=O ");
    
    
            NotificationChannel mChannel = new NotificationChannel
                    (NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
    
            mChannel.setDescription(NOTIFICATION_CHANNEL_DESCRIPTION);
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.CYAN);
            mChannel.enableVibration(true);
            notificationManager.createNotificationChannel(mChannel);
    
            NotificationChannelGroup mGroup = new NotificationChannelGroup(NOTIFICATION_GROUP_ID, NOTIFICATION_GROUP_NAME);
            notificationManager.createNotificationChannelGroup(mGroup);
    
            NotificationCompat.Builder builder = new NotificationCompat.Builder
                    (context, NOTIFICATION_CHANNEL_ID)
    
                    .setContentTitle(context.getString(R.string.notification_title))
                    .setContentText(context.getString(R.string.notification_subtext))
                    .setSmallIcon(R.drawable.ic_notification)
                    .setAutoCancel(true)
                    .setDeleteIntent(piDismiss)
                    //.setContentIntent(pIpanel)
                    .setCustomContentView(collapsedView)
                    .setCustomBigContentView(expandedView)
                    .setStyle(new NotificationCompat.DecoratedCustomViewStyle());
    
            notification = builder.build();
        }
    
        if (notificationManager != null) {
            notificationManager.notify(NOTIFICATION_ID, notification);
        }