android - 如何在不在线的情况下发送通知?

标签 android notifications

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

我看到的所有地方都与 firebase 通知和推送通知有关。

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

最佳答案

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

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

}

在 Activity 标签下面的 list 类中声明接收者..

 <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 及以上版本,您必须创建通知 channel 。在这里找到更多 Manage Channels

在上面的代码中添加:

 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);
    }

Note use drawable for small icons not use mipmap or adaptive icons.Android Oreo Notification Crashes System 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);

Note: _Context should be same as used in setreminder() method

关于android - 如何在不在线的情况下发送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52179105/

相关文章:

android - 如何消除这个 Gradle 错误?

iOS 在后台显示本地通知弹出窗口

android - 加载 jbitmap 数据

android - OpenERP (Odoo) V9 和移动框架

java - 当用户得到正确答案时递增变量

android - KitKat 上未显示 HeadsUp 通知

ios - 在 Swift 上推送通知

Android:在启动器图标上显示通知

ios - 如何在远程通知的 alertBody 中显示来自 CKRecord 的数据?

android - 如何解决 'com.android.support:appcompat-v7:28.0.0'