动态活动内容问题

时间:2014-01-07 19:38:50

标签: android google-cloud-messaging dynamic-content

我创建了一个动态列表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;
    }
}

此类用于应用的两个部分:MainActivityGCMIntentService。不用说,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种水果,不仅是苹果和橙子。但它总是显示苹果信息。

编辑2 GCMIntentService也可以打开非动态活动if(category.equals("home"))并且工作正常。

1 个答案:

答案 0 :(得分:0)

解决:

似乎在第二个参数中设置ID而不是0就可以了。

PendingIntent contentIntent = PendingIntent.getActivity(this, notificationId, intent, 0);

另外

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);