Android andriod通知不起作用,不会返回任何错误
我正在尝试创建一个简单的应用程序,每天在特定的时间发出通知,但不知何故,它不起作用,并且完全没有返回错误代码: 按下时通知应在其中启动的按钮:Android andriod通知不起作用,不会返回任何错误,android,android-studio,android-intent,android-notifications,Android,Android Studio,Android Intent,Android Notifications,我正在尝试创建一个简单的应用程序,每天在特定的时间发出通知,但不知何故,它不起作用,并且完全没有返回错误代码: 按下时通知应在其中启动的按钮: setRenider.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Calendar calendar = Calendar.getInstance();
setRenider.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,18);
calendar.set(Calendar.MINUTE,14);
calendar.set(Calendar.SECOND,0);
Intent intent = new Intent(getApplicationContext(),Notification_reciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
}
});
那么这就是通知类:
public class Notification_reciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context,ReaptingActivity.class);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setContentIntent(pendingIntent).setSmallIcon(R.drawable.ic_free_breakfast_black_24dp)
.setContentTitle("Water Time").setContentText("Drink Water").setAutoCancel(true);
notificationManager.notify(100,builder.build());
}
}
我创建了一个简单的空活动,名为it ReAcceptingActivity
我在清单中输入这些代码:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
<receiver android:name=".Notification_reciever"/>
</application>
我只需要它在notification类onReceive方法上添加一个通道。以下是我创建频道的方式:
int notificationId = 1;
String channelId = "channel-01";
String channelName = "Channel Name";
int importance = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
notificationManager.createNotificationChannel(mChannel);
}
然后在通知compat builder上,我需要它通过通道
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
您是否已调试以确定接收器是否正在运行?除此之外,从Oreo开始,Notification
s需要NotificationChannel
s在发布之前创建:。