在通知处理之前,活动从通知恢复

时间:2013-11-20 16:18:38

标签: android broadcastreceiver android-notifications

我的通知需要在点击时发送广播。问题是,在一个特定用例中,首先单击通知将恢复用户所在的最后一个活动,然后发送广播。

仅当用户通过按电源按钮暂停活动,接收通知,使用电源或主页按钮唤醒屏幕并在不解锁屏幕的情况下单击通知时,才会出现此行为。在所有其他情况下,在任何活动恢复之前发送,接收和处理广播。

在这种情况下,在收到广播之前,会调用后栈的onResume方法中的最顶层活动。

我在清单中声明我的接收器是这样的:

<permission
    android:name="com.example.broadcasttest.HANDLE_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.broadcasttest.HANDLE_MESSAGE" />
... 
    <receiver
        android:name="com.example.broadcasttest.PushNotificationBroadcastHandler"
        android:permission="com.example.broadcasttest.HANDLE_MESSAGE" >
        <intent-filter>
            <action android:name="com.example.broadcasttest.HANDLE" />
        </intent-filter>
    </receiver>

我使用辅助方法发送通知:

public static void sendNotification(String msg, Context context)
{
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(context, PushNotificationBroadcastHandler.class);
    intent.setAction(ACTION_HANDLE_NOTIFICATION);

    PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(context.getPackageManager().getApplicationLabel(context.getApplicationInfo()))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg)
            .setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL);

    builder.setContentIntent(contentIntent);

    notificationManager.notify(0, builder.build());
}

我已经尝试了这一点,实际推送通知(显然传递了ReceiverRestrictedContext)和以Activity作为上下文发送的模拟推送通知。

在这种情况下,BroadcastReceiver的作用并不重要,问题是它的onReceive方法只在最后一个活动的onResume之后被调用。

到目前为止,我已尝试使用context.getApplicationContext()而非使用context本身。我也试过开始一个活动而不是发送广播,但这似乎对有问题的行为没有影响。它也与任何特定于应用程序的代码无关,我创建了一个项目来测试它(并在此提供代码示例),所以我确定问题出在这些代码片段或Android操作系统本身。

此时任何输入都会受到赞赏。

0 个答案:

没有答案
相关问题