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

时间:2018-09-05 07:08:57

标签: android notifications

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

我到处都看到Firebase通知和推送通知。

我不希望收到这些在线通知,我需要该应用程序自行将其离线发送。这可能吗?

2 个答案:

答案 0 :(得分:3)

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

第一个创建接收方类扩展了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()方法中使用的相同

答案 1 :(得分:2)

我建议您阅读Notifications Overview。这很好地帮助您了解通知的工作原理。

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

阅读并理解。遇到任何问题时,您可以返回此处寻求解决方案。