我正在收到当地通知,要求在特定时间每天重复一次。我尝试使用一些设备进行测试,但它只适用于少数设备。请检查下面的代码,并阐明一些问题。我试过三星和LG设备。
下面是我的AlarmReceiver
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());
}
}
还有我的MainActivity
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);
}
}
的Manifest.xml
<receiver android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"
/>
在调用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());
只是一个片段,你必须涵盖所有这些构建版本。
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);
}