如何在后台触发通知?

时间:2016-02-19 07:48:30

标签: 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构建通知。

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

修改

这是我的清单

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

2 个答案:

答案 0 :(得分:2)

一周后,我终于明白了。在我的Manifest中我放了这样的东西

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

答案 1 :(得分:0)

尝试在此类应用程序标记下的清单文件中添加服务(与声明活动相同),

<service android:name="com.example.android.NotificationAlarmService">
    </service>