我正在使用推送通知,当我在应用内部时,它们工作正常。但是当应用程序处于后台时。当通知到达时,我需要它去onMessageRecieved,因为我设置的条件如下:
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
String type = data.getString("type");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
Log.d(TAG, "Type: " + type);
sendNotification(message, type);
}
private void sendNotification(String message, String type) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this);
//Set all notifcation properties etc
if (type.equals(Constants.GROUP_NOTIFICATION)) {
Intent intent = new Intent(this, MainActivity.class);
mBuilder.setContentIntent(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
}else {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(Keys.NOTIFICATION_TYPE, Constants.QUESTION_NOTIFICATION);
mBuilder.setContentIntent(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
}
}
}
对于问题推送通知,当涉及到主要活动时,通过检查意图数据,它应该重定向到questionActivity,但由于从未设置意图数据,因此它将保持主要活动状态。 所以我的问题是,如何从通知中获取数据?
答案 0 :(得分:0)
好吧,因为我没有在我的帖子上得到答案,所以我想出了自己的问题。做了一些搜索,发现有两种类型的有效载荷。数据和通知。如果你有通知有效负载,那么你的应用程序是在后台。 Android将自行创建通知,不会调用onMEssageRecieved。所以我去了我的服务器代码并简单地删除了通知有效负载部分:)现在onMessagerecieved被调用。