如何在不在线的情况下发送通知?
我正在构建一个应用程序,用户可以将其测试和作业以及任何内容放入其中.我想知道我的应用程序可能会在测试前一周和一天之内发出通知吗?
I am building an app that a user will put their tests and assignments and whatever in. I want to know will it be possible for my app to bring up a notification like a week and a day before the test?
我到处都看到Firebase通知和推送通知.
Everywhere I look its just about firebase notifications and push notifications.
我不希望收到这些在线通知,我需要该应用程序自行将其离线发送.这可能吗?
I don't want these online notification, I'll need the app to send them by itself offline. Is this possible?
让我添加一些解决方法,您可以在外面找到更多的教程.
Let me add some workaround you can find more tutorial outside..
第一个创建接收方类扩展了BroadcastReceiver
.
First create receiver class extends BroadcastReceiver
.
public class ReminderReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int Request_Code = intent.getExtras().getInt("TIME",0);
showNotification(context, MainActivity.class,
"New Notification Alert..!", "scheduled for " + Request_Code + " seconds",Request_Code);
}
public void showNotification(Context context, Class<?> cls, String title, String content,int RequestCode)
{
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent notificationIntent = new Intent(context, cls);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(cls);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(
RequestCode,PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getString(R.string.channel_name);
String description = context.getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("my_channel_01", name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"my_channel_01");
Notification notification = builder.setContentTitle(title)
.setContentText(content).setAutoCancel(true)
.setSound(alarmSound).setSmallIcon(R.drawable.ic_launcher_background)
.setContentIntent(pendingIntent).build();
notificationManager.notify(RequestCode,notification);
}
}
在活动标签下方的清单类中声明接收方.
Declare receiver in manifest class below activity tag..
<receiver android:enabled="true" android:name=".ReminderReceiver"/>
然后将提醒设置为警报管理器.
Then set reminder to alarm manager.
public void setReminder(Context context,Class<?> cls,int sec)
{
Intent intent = new Intent(context, cls);
intent.putExtra("TIME",sec);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, sec, intent,
PendingIntent.FLAG_ONE_SHOT);/* Find more about flags: https://developer.android.com/reference/android/app/PendingIntent */
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (sec * 1000), pendingIntent );//Add time in milliseconds. if you want to minute or hour mutiply by 60.. For ex: You want to trigger 5 Min then here you need to change 5 * 60 * 1000
}
最后设置提醒
setReminder(_Context,ReminderReceiver.class,time);
已更新
要支持android 8.0及更高版本,您必须创建通知通道.在此处找到更多信息 管理频道
在上面的代码中添加它:
Add this in above code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getString(R.string.channel_name);
String description = context.getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("my_channel_01", name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager.createNotificationChannel(channel);
}
请注意为小图标使用drawable而不使用mipmap或自适应图标. Android Oreo通知崩溃系统界面
要取消预定的通知
public void cancelReminder(Context context,Class<?> cls)
{
Intent intent1 = new Intent(context, cls);
intent1.putExtra("TIME",time);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
time, intent1, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if(pendingIntent != null) {
am.cancel(pendingIntent);
}
}
并使用上述方法删除
cancelReminder(_Context,ReminderReceiver.class);
注意:_Context应该与
setreminder()
方法中使用的相同
Note: _Context should be same as used in
setreminder()
method