【问题标题】:GcmListenerService when running in background differentGcmListenerService 在后台运行时不同
【发布时间】:2016-03-07 15:25:58
【问题描述】:

我按照本教程创建了一个新的 GCM 侦听器服务: http://www.appifiedtech.net/2015/08/01/android-gcm-push-notification-example/

监听服务的代码:

@Override
public void onMessageReceived(String from, Bundle data) {
    super.onMessageReceived(from, data);
    String msg = data.getString("message");
    Log.d(TAG,"GCM Message received is "+msg);
    // Notifying to user code goes here
    notifyUser(getApplicationContext(),msg);
}

public void notifyUser(Context context,String data){
    Intent intent = new Intent(context, NotificationActivity.class);
    intent.putExtra("data", data);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
            Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setAutoCancel(true);
    builder.setContentTitle("New Notification");
    builder.setContentIntent(pendingIntent);
    builder.setContentText(data);
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    builder.setSound(uri);
    notificationManager.notify(countNotification++, builder.build());
    Log.v(TAG,"count "+countNotification);
}

当应用程序正在运行(前台)时,它可以正常工作并按应有的方式启动通知活动。

但是,当它在后台运行时,我会收到通知,但标题和正文是在我的服务器发件人应用程序中定义的,点击它会将我带到主活动。

  1. 这实质上意味着当它在后台运行时,其他东西会处理通知?我应该实施另一个处理程序来管理该通知并将用户发送到正确的活动吗?

  2. 当我收到此通知时屏幕不会唤醒,手机上的 LED 也不会像其他应用程序的通知一样亮起。你是怎么做到的?

(权限、服务和接收者在清单中定义,如教程中所述)

【问题讨论】:

    标签: android android-activity google-cloud-messaging gcmlistenerservice


    【解决方案1】:
    1. 您的应用在后台运行时是否有其他东西处理通知?

    SO question 可以帮助您回答有关 GCM 侦听器服务的问题

    1. 关于收到通知时屏幕不唤醒的问题。

    使用 ACQUIRE_CAUSES_WAKEUP

    普通唤醒锁实际上不会打开照明。相反,它们会导致照明一旦打开(例如来自用户活动)就保持打开状态。当获取 WakeLock 时,此标志将强制屏幕和/或键盘立即打开。一个典型的用途是通知对用户来说很重要,可以立即看到。

    你可以访问这个SO question了解如何使用它。

    【讨论】:

    • 感谢 KENdi,这两个是我自己想出来的,但是我仍然无法让 GCMReceiver 上的 LED 亮起。 builder.setLights(Color.BLUE,500,500); 对此有何想法?
    • 让 LED 也能正常工作。由于某种原因,蓝色没有工作,而绿色很好。简而言之,对于我的问题,您需要制作与 GCMReceiver 的 GCMListenerService 实现几乎相同的内容。要唤醒处理器,您需要使用 PowerManager newWakeLock 和 Acquire/Release。
    【解决方案2】:

    你删除

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

    使用intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    在 NotificationActivity 类中,需要在打开 NotificationActivity 时实现 onNewIntent() 回调。

    【讨论】:

    • 使用 FLAG_ACTIVITY_SINGLE_TOP 没有区别。我还在 NotificationActivity 中覆盖了 onNewIntent() ,但没有结果,和以前一样。当应用程序在后台时,当我点击通知时只打开 MainActivity
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    相关资源
    最近更新 更多