如何在后台触发通知? [英] How to trigger Notification in background?
问题描述
我试图从我设置的时间开始,每 15 分钟触发一次通知.目前,我的应用程序仅在我的应用程序打开或在最近的应用程序中时才通知,但是当我关闭我的应用程序时,通知不再起作用,但是当我再次打开我的应用程序时,它会触发通知.根据我的研究,我需要一项服务.所以我有 3 个文件.
I'm trying to trigger a notification every 15 minutes, starting from the time I set. Currently my app notifies only when my application is open or when it is in the recent applications but when i closed my application the notification doesn't work any longer but when i open again my app it will trigger the notification. Based on my research I need a Service. So I have 3 files.
在我的 MainActivity 中,我设置了闹钟
In my MainActivity, i set the alarm
private void scheduleNotification(int week){
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
notificationIntent.putExtra("notifId", getResources().getInteger(R.integer.notification_id));
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, getResources().getInteger(R.integer.notification_id), notificationIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, week);
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
}
在 MainActivity 中,我调用了一个 BroadcastReceiver,它是 NotificationPublisher.这是我的 NotificationPublisher
in the MainActivity i call a BroadcastReceiver which is the NotificationPublisher. Here is my NotificationPublisher
public class NotificationPublisher extends BroadcastReceiver {
final String TAG = "NotificationPublisher";
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, NotificationAlarmService.class);
service.putExtra("notifId", intent.getIntExtra("notifId", 0));
context.startService(service);
}
然后我使用 BroadcastReceiver 启动一个服务,它是 NotificationAlarmService,这里我使用 NotificationCompat.Builder
构建一个通知.
then I start a service by using the BroadcastReceiver which is the NotificationAlarmService, here i build a Notification using NotificationCompat.Builder
.
public class NotificationAlarmService extends Service {
. . . .
@Override
public int onStartCommand(Intent intent,int flag, int startId)
{
notificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, MainActivity.class);
pendingIntent = PendingIntent.getActivity(this, intent.getIntExtra("notifId", 0), mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("App");
builder.setContentText("Notify Me");
builder.setAutoCancel(true);
builder.setSmallIcon(getNotificationIcon());
builder.setContentIntent(pendingIntent);
notificationManager.notify(intent.getIntExtra("notifId", 0), builder.build());
return super.onStartCommand(intent, flag, startId);
}
我已经尝试返回 START_STICKY
而不是 super.onStartCommand
我知道当我使用服务时,即使用户关闭了应用程序,它也会在后台运行.我还尝试更改 this
并在服务中使用 getBaseContext()
但这不会触发通知.我不知道我是否遗漏了什么.先感谢您.
I already tried returning START_STICKY
instead of super.onStartCommand
I understand that when i used a service it will run in the background even if the user closed the application. I also tried changing this
and used getBaseContext()
in the service but this it doesn't trigger the notification. I don't know if i'm missing something. Thank you in advance.
编辑
这是我的清单
<receiver android:name=".NotificationPublisher"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".NotificationAlarmService"
android:enabled="true">
</service>
推荐答案
一周后,我终于搞定了.在我的清单中,我放了这样的东西
After a week, I finally got it. In my Manifest i put something like this
<receiver android:name=".NotificationPublisher">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name=".NotificationAlarmService"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="NOTIFICATION_SERVICE" />
</intent-filter>
</service>
我删除了接收器中的 enabled="true"
并添加了 QUICKBOOT_POWERON
.然后在 NOTIFICATION_SERVICE
服务中添加一个操作,并像这样在我的服务中调用它
i've removed the enabled="true"
in the receiver and also add QUICKBOOT_POWERON
. Then add an action in the service which is NOTIFICATION_SERVICE
and call it in my service like this
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
这篇关于如何在后台触发通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!