Android:推送通知点击后打开外部浏览器

时间:2016-01-06 11:45:01

标签: android browser push-notification google-cloud-messaging android-pendingintent

我在锁定屏幕上处理推送通知时遇到问题。

我在我的应用中创建了一个GCM模块, MyAppGcmListenerService 类扩展 GcmListenerService ,并在 onMessageReceived 方法中处理了正在进行的捆绑

@Override
    public void onMessageReceived(String from, Bundle data) {
        logger.info("MyAppGcmListenerService: onMessageReceived: from = " + from + "; data = " + data.toString());
        sendNotification(createMessage(data));
    }

    private void sendNotification(Message message) {
    Intent intent = null;

    if (TextUtils.isEmpty(message.getUrl())) {
        intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    } else {
        intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setData(Uri.parse(Utils.buildUrlWithExtraInfo(message.getUrl(), Prefs.restoreGuid())));
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notif_small_icon)
            .setColor(getBaseContext().getResources().getColor(R.color.notif_bg_color))
            .setContentTitle(message.getTitle())
            .setContentText(message.getParagraph())
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, notificationBuilder.build());
}

当我点击通知栏中的推送通知时,一切正常 - 我收到非空网址的推送和外部浏览器打开我的网址。当我在锁定屏幕(Android 5+)上点击通知时会出现问题 - 在这种情况下,我被建议“滑动屏幕解锁”,然后没有运行浏览器来打开网址。

有没有人知道如何解决这个问题?

UPD。在这两种情况下,启动应用(MainActivity.class)运行良好:在通知栏和锁定屏幕上点击通知时。

1 个答案:

答案 0 :(得分:2)

您是否将Chrome用作设备上的默认浏览器? 如果是这样,这可能是Chrome在锁定屏幕中忽略收到的意图的问题,如here

我仍然试图为此找到解决方法,我还尝试将Intent发送到虚拟活动,该活动已完成并在调用onResume()时启动Chrome(当活动在前面时调用onResume并且对用户可见),但Chrome仍然忽略了这个意图...

相关问题