【问题标题】:How to send notification without being online?如何在不在线的情况下发送通知?
【发布时间】:2018-09-05 07:08:56
【问题描述】:

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

我看到的所有地方都只是关于 Firebase 通知和推送通知。

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

【问题讨论】:

  • 你需要为它使用AlaramManager
  • 是的,您可以通过使用警报管理器安排时间来触发本地推送通知..
  • @MohamedMohaideenAH 谢谢您的回复。您知道的任何地方都有很好的教程或对如何做有很好的见解的地方?
  • @RavindraKushwaha 也谢谢你,我向上滚动时才看到你的评论,请看我上面的评论。

标签: android notifications


【解决方案1】:

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

首先创建接收器类扩展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 及以上版本,您必须创建通知通道。在这里找到更多 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);
    }

注意小图标使用drawable不使用mipmap或自适应图标。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);

注意:_Context 应与setreminder() 方法中使用的相同

【讨论】:

  • 您好,我已按照您的代码进行操作。我没有收到通知。我上传了我的项目的 RAR,你能检查我做错了什么吗?谢谢你。 drive.google.com/open?id=1lhCIxLpF_JSvsBa_a-fu1X_ZfAUpqGrW
  • 浮动操作按钮中包含设置setReminder(_Context,ReminderReceiver.class,time);
  • 您正在检查哪个版本的 api?
  • minSdkVersion 22targetSdkVersion 27compileSdkVersion 27。我正在使用 android 8 测试华为 P10。
  • 您必须从 8.0 开始使用通知渠道,您可以在此处找到更多信息 developer.android.com/guide/topics/ui/notifiers/…
【解决方案2】:

我建议你阅读Notifications Overview。这可以帮助您了解通知的工作原理。

现在要构建通知,这里是Here is the official documentation for notification

阅读并理解。当你遇到任何问题时,你可以回到这里寻求解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2014-02-23
    • 2019-01-26
    • 2016-05-03
    • 2020-10-19
    • 1970-01-01
    • 2015-04-24
    相关资源
    最近更新 更多