android - 如何在后台触发通知?

标签 android service notifications alarmmanager

我尝试从我设置的时间开始,每 15 分钟触发一次通知。目前,我的应用程序仅在我的应用程序打开时或在最近的应用程序中时通知,但当我关闭我的应用程序时,通知不再有效,但当我再次打开我的应用程序时,它将触发通知。根据我的研究,我需要一项服务。所以我有 3 个文件。

在我的 MainActivity 中,我设置了闹钟

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

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 构建一个 Notification。

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() 但这不会触发通知。我不知道我是否遗漏了什么。先感谢您。

编辑

这是我的 list

    <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>

最佳答案

一个星期后,我终于搞定了。在我的 list 中,我放了这样的东西

<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 并像这样在我的服务中调用它

NotificationManager mNotificationManager = (NotificationManager)
                    this.getSystemService(Context.NOTIFICATION_SERVICE); 

关于android - 如何在后台触发通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35500049/

相关文章:

html - 如何在 Angular Material 7 中使用 CSS 操纵 mat-snack-bar 模板的外观

iOS 警报横幅/标签?

java - 将大文件上传到 Google 云端硬盘

java - 启动相机 Activity Cordova 插件

java - 将nodejs项目导入eclipse IDE

android - Activity 泄露了最初在这里注册的 IntentReceiver。您是否错过了对 unregisterReceiver() 的调用?

Android Gallery 纯文本

android - 检查用户是否已授予 NotificationListener 访问我的应用程序的权限

windows - 使用 Windows 任务计划程序调用 Windows 服务时出现问题

ios - 如何处理来自应用程序委托(delegate)的 UIViewcontroller 中的通知?