【发布时间】: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);
}
当应用程序正在运行(前台)时,它可以正常工作并按应有的方式启动通知活动。
但是,当它在后台运行时,我会收到通知,但标题和正文是在我的服务器发件人应用程序中定义的,点击它会将我带到主活动。
这实质上意味着当它在后台运行时,其他东西会处理通知?我应该实施另一个处理程序来管理该通知并将用户发送到正确的活动吗?
当我收到此通知时屏幕不会唤醒,手机上的 LED 也不会像其他应用程序的通知一样亮起。你是怎么做到的?
(权限、服务和接收者在清单中定义,如教程中所述)
【问题讨论】:
标签: android android-activity google-cloud-messaging gcmlistenerservice