【发布时间】:2014-01-25 15:28:59
【问题描述】:
我创建了一个动态列表 Activity,它从 PHP 文件(通过 HTTP)获取其内容。
所以我做了这样的事情:
public class SectionFactory {
public static Intent getAppleList(Context ctx) {
Intent intent = new Intent(ctx ,MainEntryListActivity.class);
intent.putExtra("phpFileName","getApple.php");
intent.putExtra("jsonArrayName","apples");
intent.putExtra("pageTitle","Apples");
return intent;
}
public static Intent getOrangeList(Context ctx) {
Intent intent = new Intent(ctx ,MainEntryListActivity.class);
intent.putExtra("phpFileName","getOrange.php");
intent.putExtra("jsonArrayName","oranges");
intent.putExtra("pageTitle","Oranges");
return intent;
}
}
这个类用于应用程序的两个部分:MainActivity 和 GCMIntentService。不用说,MainActivity 是应用程序的主要活动。它包含 2 个按钮,一个用于苹果,一个用于橙子。这里的一切都很好。
问题出在GCMIntentService。它是一个使用 GCM 处理推送通知的类。我的想法是我从推送中得到一个JSON 响应,然后我根据JSON 中的值来决定通知是将我发送到苹果列表还是橙色列表。
问题是,它总是重定向到苹果。
public class GcmIntentService extends IntentService {
public static final String TAG = GcmIntentService.class.getSimpleName();
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
} else if (GoogleCloudMessaging. MESSAGE_TYPE_DELETED.equals(messageType)) {
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
sendNotification(extras.getString("message"));
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
try {
Log.i(TAG,"Message: " + msg);
JSONObject obj = new JSONObject(msg);
String message = obj.getString("content");
String category = obj.getString("category");
int icon = -1;
int notificationId = -1;
Intent intent = null;
if(category.equals("apples")) {
icon = R.drawable.apples;
intent = SectionFactory.getApples(this);
notificationId = 1;
}
else if(category.equals("oranges")) {
icon = R.drawable.oranges;
intent = SectionFactory.getOranges(this);
notificationId = 2;
}
Log.d(TAG,"Category: " + category);
Log.d(TAG,"notificationId: " + notificationId);
Log.d(TAG,"PHP filename: " + intent.getStringExtra("phpFileName"));
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(icon)
.setContentTitle("Fruits")
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message);
mBuilder.setContentIntent(contentIntent);
Notification notif = mBuilder.build();
notif.defaults |= Notification.DEFAULT_SOUND;
notif.defaults |= Notification.DEFAULT_VIBRATE;
//notif.flags |= Notification.DEFAULT_LIGHTS;
notif.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(notificationId, notif);
}
catch(Exception e) { e.printStackTrace(); }
}
}
我也检查了这些日志
Log.d(TAG,"Category: " + category);
Log.d(TAG,"notificationId: " + notificationId);
Log.d(TAG,"PHP filename: " + intent.getStringExtra("phpFileName"));
他们打印出橙子信息,但页面是用苹果信息创建的。
为什么,有什么类型的缓存或我缺少的东西吗?
编辑:大约有 5 种水果,不仅是苹果和橙子。但它总是显示苹果信息。
EDIT 2:GCMIntentService 也可以打开一个非动态活动if(category.equals("home")) 并且工作正常。
【问题讨论】:
标签: android google-cloud-messaging dynamic-content