【发布时间】:2014-07-05 17:21:34
【问题描述】:
请帮帮我。我被 GCM 推送消息卡住了。 一切都很完美,但是当我试图在下一个屏幕上显示我的信息时,我总是会变老一个或第一个。 但是如果我检查我的日志猫,我总是从服务器收到新消息。那么我没有得到的问题在哪里。我在stackoverflow上尝试了很多代码。这是我的代码的 sn-p-
// this is the my service class
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
super(SENDER_ID);
}
// this is the onMessage revive method
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "new message= ");
String message = intent.getExtras().getString("message");
generateNotification(context, message);
System.out.println(message+"++++++++++1");
}
// this is the notification method
private void generateNotification(Context context, String message) {
System.out.println(message+"++++++++++2");
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
String subTitle = "Important News for you!";
Intent notificationIntent = new Intent(context, NotificationView.class);
notificationIntent.putExtra("content", message);
//PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.setLatestEventInfo(context, title, subTitle, intent);
//To play the default sound with your notification:
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
public class NotificationView extends Activity {
// and this is the my next activity where i am displaying push message-
Intent intent=getIntent();
stringValue=intent.getStringExtra("content");
System.out.println(stringValue);
我已经试过了-
PendingIntent 意图 = PendingIntent.getActivity(context, 0,notificationIntent, 0);
还有这个—— PendingIntent 意图 = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
两者都不适合我。
【问题讨论】:
-
System.out.println(message+"++++++++++1");和 System.out.println(message+"++++++++++2");是给我正确的信息。但在下一页我只是变老了。
-
同样的代码在另一个应用程序中对我来说工作正常。
标签: android push google-cloud-messaging