【问题标题】:android C2DM notifications not working prior to android 4?android C2DM 通知在 android 4 之前不起作用?
【发布时间】:2012-04-18 16:37:33
【问题描述】:

我试图让推送通知适用于我的 Android 应用。服务器似乎没问题,因为我在我的 android 4 设备上收到通知。但是我有其他 android 2.2.1 和 2.3.4 的设备没有收到通知。

这是我的 C2DMReceiver:

package vex.android;

import java.io.IOException;

import vex.android.settings.Local;
import vex.android.tool.Resources;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.android.c2dm.C2DMBaseReceiver;

public class C2DMReceiver extends C2DMBaseReceiver {

    public C2DMReceiver() {
        super(Local.PushNotificationEmail);
    }

    @Override
    public void onError(Context context, String errorId) {
        Log.e("VEX-PUSHNOTIFICATION", "Error " + errorId);
    }

    @Override
    protected void onMessage(Context context, Intent intent) {

        String saleTitle = Resources.getString("pushnotificationtitle", context); 
        String saleMessage = intent.getStringExtra("salemessage");
        String SaleId = intent.getStringExtra("saleid");
        String isMultiSale = intent.getStringExtra("ismultisale");

        Boolean multisale = (isMultiSale != null && isMultiSale.length()>0) ?  Boolean.parseBoolean(isMultiSale.trim()) : false;
        Integer saleid = (SaleId != null && SaleId.length()>0) ? Integer.parseInt(SaleId.trim()) : -1;
        if(saleMessage == null || saleMessage.length() <= 0 ) saleMessage = Resources.getString("pushnoticationmessage", context);
        createNotification(context, saleTitle, saleMessage, saleid, multisale);
    }

    public void createNotification(Context context,String SaleTitle, String SaleMessage, Integer saleid, Boolean multisale) {

        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.applicationicon,
                "Message received", System.currentTimeMillis());
        // Hide the notification after its selected
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        Intent intent = new Intent(context, MainApplication.class);
        intent.putExtra("saleid", saleid);
        intent.putExtra("ismultisale", multisale);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // without flag a changed saleid wont be passed
        notification.setLatestEventInfo(context, SaleTitle, SaleMessage, pendingIntent);
        notificationManager.notify(saleid, notification);
    }

    @Override
    public void onRegistered(Context context, String registrationId) 
    throws IOException 
    {
        Local.setRegistrationId(registrationId);
    }

    @Override
    public void onUnregistered(Context context) 
    {
            Log.i("VEX-DEBUG", "successfully unregistered with C2DM server");
    }

}

我认为问题存在,因为如果我手动发送通知(使用 curl),它不适用于 android 2.2 和 2.3。任何想法?谢谢

【问题讨论】:

    标签: android push-notification android-c2dm


    【解决方案1】:

    我在旧 Android 版本的设备上使用 C2DM 没有任何问题。

    我建议您使用更多设备进行测试,并检查您的代码 - 问题不是缺乏对旧设备的 C2DM 支持的问题 - 它适用于从 v2.2 开始的 Android。

    【讨论】:

      【解决方案2】:

      C2DM 使用 Google 消息服务。 GTalk 也使用此服务。有时可能会关闭此服务。要检查所有相关信息,只需输入此代码 - *#*#8255#*#*

      C2DM 在 android >= 2.2 的设备上可用

      【讨论】:

      • 顺便说一句 - 您应该始终在设备上登录 Google 帐户以接收 C2D 消息
      猜你喜欢
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多