Android:在应用关闭时从后台推送dinamic通知

时间:2016-06-03 20:51:40

标签: java android android-intent notifications android-service

我需要从后台发送通知,即使用户从RAM中删除了应用程序,从意图中获取信息(以放入通知)作为额外内容。

目前,如果应用程序在后台打开或打开,它可以正常运行,但是当应用程序从最近的应用程序关闭时(从RAM中删除),它无法获得额外的功能。

这是创建后台服务的活动,并将我需要传输的ID作为Extra。

AddEvent.java

// Gets the ID after it was generated by the database
        int ID = newEvent.getID();

        if (newEvent.hasNotification()) {
            // Creates the intent that starts the background service
            Intent serviceIntent = new Intent(this, NotificationService.class);

            // Puts the ID and the Notification Time as Extras and starts the service
            serviceIntent.putExtra(EXTRA_NOTIFICATION_ID,ID);
            serviceIntent.putExtra(EXTRA_NOTIFICATION_TIME,notificationDate.getTimeInMillis());
            startService(serviceIntent);
        }

此活动启动我扩展的IntentService。

NotificationService.java

public class NotificationService extends IntentService {

public NotificationService() {
    super("Notification Service");
}

@Override
protected void onHandleIntent(Intent workIntent) {

    // Create the intent that is going to push the notification, giving it the previous bundle
    Intent notificationIntent = new Intent(this, NotificationPusher.class);

    long notificationTime = workIntent.getLongExtra(ActivityAddEvent.EXTRA_NOTIFICATION_TIME,-1);
    int ID = workIntent.getIntExtra(ActivityAddEvent.EXTRA_NOTIFICATION_ID,-1);
    Log.d("ID Service",""+ID);
    notificationIntent.putExtra(ActivityAddEvent.EXTRA_NOTIFICATION_ID,ID);

    PendingIntent pusher = PendingIntent.getBroadcast(this, UniqueID.getUniqueID(),
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    //TODO : Can't get the Extras in Pusher

    // Sets the alarm for the designed date and time
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP,notificationTime,pusher);
}

public static class UniqueID {
    private final static AtomicInteger uniqueID = new AtomicInteger(0);
    public static int getUniqueID() {
        return uniqueID.incrementAndGet();
    }
}

}

获取通知时间并将其设置为警报管理器,以便在所需时间推送通知。

正确 ID和TIME(只要)然后将ID放入新意图中,即发出通知的通知推送器。

NotificationPusher.java

public class NotificationPusher extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent workIntent) {

    // Gets the event from the database using the ID received in the intent
    int ID = workIntent.getIntExtra(ActivityAddEvent.EXTRA_NOTIFICATION_ID, -1);
    Log.d("ID Pusher", "" + ID);


    EventManager eventManager = EventManager.getInstance(context);
    Event event = null;
    try {
        event = eventManager.getEvent(ID);
    } catch (SQLException e) {
        // TODO: Add error for id not found
        e.printStackTrace();
    }

    if (event != null) {
        String notificationTitle;
        String notificationText;
        // Sets the notification
        if (event.hasSubject()) {
            notificationTitle = event.getSubject() + "'s " + event.getType().toString();
            notificationText = context.getString(R.string.event_notification_default_text);
        } else {
            notificationTitle = event.getType().toString();
            notificationText = event.getTitle();
        }

        // Create the intent that is gonna be triggered when the notification is clicked and add to the stack
        Intent notificationIntent = new Intent(context, ActivitySingleEvent.class);
        notificationIntent.putExtra(ActivityAddEvent.EXTRA_NOTIFICATION_ID, ID);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, NotificationService.UniqueID.getUniqueID(),
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        // Gets the default sound for notifications
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        // Create the notification with a title,icon,text,sound and vibration
        NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_alarm_white_24dp)
                .setContentTitle(notificationTitle)
                .setContentText(notificationText)
                .setContentIntent(pendingIntent)
                // Notification auto cancel itself when clicked
                .setAutoCancel(true)
                .setSound(uri)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                .setLights(Color.BLUE, 3000, 3000);

        // Build the notification and issue it
        Notification notification = nBuilder.build();
        NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nManager.notify(ID, notification);
    }
    else {
        Toast.makeText(context,"null event",Toast.LENGTH_SHORT);
    }
}

}

在Notification Pusher的顶部,它尝试从Extras获取ID,但每次从ram中删除应用程序时,它都会获得默认值(-1)。 我不知道如何传递此ID。

1 个答案:

答案 0 :(得分:0)

您需要将服务作为前台服务,这很可能需要永久性的系统通知,但在应用程序本身关闭时会保持活动状态。

https://developer.android.com/guide/components/services.html#Foreground