首页 > 解决方案 > 如何在不在线的情况下发送通知?

问题描述

我正在构建一个应用程序,用户将在其中放置他们的测试和作业等等。我想知道我的应用程序是否有可能在测试前一周和一天发出通知?

我到处看它只是关于firebase通知和推送通知。

我不想要这些在线通知,我需要应用程序自己离线发送它们。这可能吗?

标签: androidnotifications

解决方案


让我添加一些解决方法,您可以在外面找到更多教程..

首先创建接收器类扩展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);
}

}

在活动标签下方的清单类中声明接收者..

 <receiver android:enabled="true" android:name=".ReminderReceiver"/>

然后将提醒设置为警报管理器。

 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 及更高版本,您必须创建通知通道。在此处查找更多 信息

在上面的代码中添加:

 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 通知崩溃系统 UI

取消预定通知

 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()方法中使用的相同


推荐阅读