Android:在应用程序关闭时从后台推送动态通知

Posted

技术标签:

【中文标题】Android:在应用程序关闭时从后台推送动态通知【英文标题】:Android: Push dynamic notifications from background while app is closed 【发布时间】:2016-10-04 00:59:03 【问题描述】:

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

目前,如果应用程序在后台打开或打开,它可以工作,但是当应用程序从最近的应用程序中关闭(从 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 放入新 Intent 中,即发出通知的通知推送器。

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

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

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

所以我需要把我的 IntentService 改成普通的 Service 吗?我已经尝试过了,但是如果它不是 Intent,我该如何为服务添加额外内容?编辑:还有其他不需要永久系统通知的方法吗?

以上是关于Android:在应用程序关闭时从后台推送动态通知的主要内容,如果未能解决你的问题,请参考以下文章

当应用程序在 iOS 中关闭且应用程序未在后台运行时从推送通知中获取数据

应用关闭时未收到 Android 推送通知

Android推送通知,当应用程序关闭时我会得到不同的风格

关闭推送通知

应用程序完全关闭时不出现 Android 推送通知(使用 node-gcm)

应用关闭时从通知中显示 UIAlertView 的问题