我正在尝试在android中设置每日通知。使用警报接收器是我的代码。
public class DrawerActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_menu);
Intent alarmIntent = new Intent(DrawerActivity.this, LocalNotificationReceiver.class);
dailyNotificationPendingIntent = PendingIntent.getBroadcast(DrawerActivity.this, 0, alarmIntent, 0);
dailyNotificationManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 22);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
dailyNotificationManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, dailyNotificationPendingIntent);
}
}
这是我的闹钟接收器
public class LocalNotificationReceiver extends WakefulBroadcastReceiver {
private final static String TAG = "LocalNotificationReceiv";
public LocalNotificationReceiver(){}
Realm realm;
private NotificationUtils notificationUtils;
String currentStartDate;
SimpleDateFormat dateFormat;
public NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);
@Override
public void onReceive(Context context, Intent intent) {
realm = Realm.getDefaultInstance();
Calendar calStartDate = Calendar.getInstance();
dateFormat = new SimpleDateFormat("yyyy-MM-dd");
currentStartDate = dateFormat.format(calStartDate.getTime());
SweatPointFitnessDB sweatPointFitnessDB = realm.where(SweatPointFitnessDB.class)
.equalTo("forday",currentStartDate)
.findFirst();
String title;
String alert;
try{
if (sweatPointFitnessDB.getStepCount() != 0) {
Log.i(TAG, "Yout step count today notification: " + sweatPointFitnessDB.getStepCount());
title = "Hey you did " + numberFormat.format(sweatPointFitnessDB.getStepCount()) + " steps today!!";
alert = "Kudos";
} else {
title = "You were inactive today!!";
alert = "Buckle up";
}
Log.i(TAG, "inside local notification receiver");
Intent resultIntent = new Intent(context, MainActivity.class);
showNotificationMessage(context, title, alert, resultIntent);
}catch (Exception e){
}
}
private void showNotificationMessage(Context context, String title, String message, Intent intent) {
notificationUtils = new NotificationUtils(context);
// intent.putExtras("title",title);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessageNew(title, message, intent);
}
}
最后这是我的通知活动。
public void showNotificationMessageNew(String title, String message, Intent intent){
if(TextUtils.isEmpty(message)){
return;
}
int icon = R.drawable.push_icon;
int mNotificationId = 100;
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
Notification notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setStyle(inboxStyle)
.setContentIntent(resultPendingIntent)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(mNotificationId, notification);
}
现在问题是我每天晚上10点收到通知,这很好。但是点击它并进入应用程序后,会一次又一次地显示通知。
答案 0 :(得分:2)
您每次都会从DrawerActivity触发本地通知,保存本地通知已安排的首选项,并在下次安排之前进行检查。
目前每次调用DrawerActivity onCreate时都会触发通知。