GcmListenerService when 运行 in background different
GcmListenerService when running in background different
我已经按照本教程创建了一个新的 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);
}
当应用程序处于 运行(前台)时,这可以正常工作并按预期启动通知 Activity。
然而,当它在后台 运行 时,我收到了通知,但标题和 body 是在我的服务器发件人应用程序中定义的,点击它会将我带到 Main Activity。
这实质上意味着当它 运行 在后台时,其他东西会处理通知?我应该实施另一个处理程序来管理该通知并将用户发送到正确的 activity?
当我收到此通知时,屏幕不会唤醒,phone 上的 LED 也不会像其他应用程序的通知那样亮起。你是怎么做到的?
(权限、服务和接收者在教程中描述的清单中定义)
您删除
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
使用intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
在NotificationActivity class 中,当你打开NotificationActivity 存在堆栈时,你需要执行onNewIntent() 回调。
- In problem regarding when your apps running in the background something else handles the notification?
此 可以帮助您回答有关 GCM 侦听器服务的问题
- In the problem regarding to the screen that doesn't wake when you receive notification.
使用ACQUIRE_CAUSES_WAKEUP
普通唤醒锁实际上不会打开照明。相反,它们会使照明在打开后保持打开状态(例如来自用户 activity)。当获取唤醒锁时,此标志将强制屏幕 and/or 键盘立即打开。一个典型的用途是通知,这对用户来说很重要,可以立即看到。
您可以在如何使用它SO question中访问它。
我已经按照本教程创建了一个新的 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);
}
当应用程序处于 运行(前台)时,这可以正常工作并按预期启动通知 Activity。
然而,当它在后台 运行 时,我收到了通知,但标题和 body 是在我的服务器发件人应用程序中定义的,点击它会将我带到 Main Activity。
这实质上意味着当它 运行 在后台时,其他东西会处理通知?我应该实施另一个处理程序来管理该通知并将用户发送到正确的 activity?
当我收到此通知时,屏幕不会唤醒,phone 上的 LED 也不会像其他应用程序的通知那样亮起。你是怎么做到的?
(权限、服务和接收者在教程中描述的清单中定义)
您删除
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
使用intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
在NotificationActivity class 中,当你打开NotificationActivity 存在堆栈时,你需要执行onNewIntent() 回调。
- In problem regarding when your apps running in the background something else handles the notification?
此
- In the problem regarding to the screen that doesn't wake when you receive notification.
使用ACQUIRE_CAUSES_WAKEUP
普通唤醒锁实际上不会打开照明。相反,它们会使照明在打开后保持打开状态(例如来自用户 activity)。当获取唤醒锁时,此标志将强制屏幕 and/or 键盘立即打开。一个典型的用途是通知,这对用户来说很重要,可以立即看到。
您可以在如何使用它SO question中访问它。