Android GCM-在客户端创建通知时崩溃

Android GCM-在客户端创建通知时崩溃,android,google-cloud-messaging,Android,Google Cloud Messaging,我有一个例外: java.lang.NoSuchMethodError: android.app.Notification$Builder.setProgress at android.support.v4.app.NotificationCompatIceCreamSandwich.a dd(NotificationCompatIceCreamSandwich.java:31) at android.support.v4.app.NotificationCompat$Notif

我有一个例外:

java.lang.NoSuchMethodError: android.app.Notification$Builder.setProgress at android.support.v4.app.NotificationCompatIceCreamSandwich.a
dd(NotificationCompatIceCreamSandwich.java:31)
        at android.support.v4.app.NotificationCompat$NotificationCompatImplIceCreamSandwich.build(NotificationCompat.java:104)
        at android.support.v4.app.NotificationCompat$Builder.build(NotificationCompat.java:558)
        at com.problemio.GCMIntentService.generateNotification(GCMIntentService.java:132)
        at com.problemio.GCMIntentService.onMessage(GCMIntentService.java:71)
        at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:132)
        at android.os.HandlerThread.run(HandlerThread.java:60)
这是我的gcminentservice.java

package com.problemio;

import static com.google.android.gcm.GCMConstants.ERROR_SERVICE_NOT_AVAILABLE;
import static com.google.android.gcm.GCMConstants.EXTRA_ERROR;
import static com.google.android.gcm.GCMConstants.EXTRA_REGISTRATION_ID;
import static com.google.android.gcm.GCMConstants.EXTRA_SPECIAL_MESSAGE;
import static com.google.android.gcm.GCMConstants.EXTRA_TOTAL_DELETED;
import static com.google.android.gcm.GCMConstants.EXTRA_UNREGISTERED;
import static com.google.android.gcm.GCMConstants.INTENT_FROM_GCM_LIBRARY_RETRY;
import static com.google.android.gcm.GCMConstants.INTENT_FROM_GCM_MESSAGE;
import static com.google.android.gcm.GCMConstants.INTENT_FROM_GCM_REGISTRATION_CALLBACK;
import static com.google.android.gcm.GCMConstants.VALUE_DELETED_MESSAGES;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import com.google.android.gcm.GCMBaseIntentService;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import utils.GCMConstants;
import utils.SendEmail;

public class GCMIntentService extends GCMBaseIntentService 
{
    public GCMIntentService() 
    {
            super(ProblemioActivity.SENDER_ID);
    }

    @Override
      protected void onRegistered(Context ctxt, String regId) {
        Log.d(getClass().getSimpleName(), "onRegistered: " + regId);
        Toast.makeText(this, regId, Toast.LENGTH_LONG).show();
      }

      @Override
      protected void onUnregistered(Context ctxt, String regId) {
        Log.d(getClass().getSimpleName(), "onUnregistered: " + regId);
      }

      @Override
      protected void onMessage(Context ctxt, Intent message) 
      {
        Bundle extras=message.getExtras();

        for (String key : extras.keySet()) 
        {
            //sendEmail( "Key: " + key , "value: " + extras.getString(key) );

          Log.d(getClass().getSimpleName(),
                String.format("onMessage: %s=%s", key,
                              extras.getString(key)));
        }

        generateNotification(ctxt, extras.getString("message"), "New Message");
      }

        // Subject , body
        public void sendEmail( String subject , String body )
        {
            String[] params = new String[] { "http://www.problemio.com/problems/send_email_mobile.php", subject, body };

            SendEmail task = new SendEmail();
            task.execute(params);               
        }    

      @Override
      protected void onError(Context ctxt, String errorMsg) {
        Log.d(getClass().getSimpleName(), "onError: " + errorMsg);
      }

      @Override
      protected boolean onRecoverableError(Context ctxt, String errorMsg) {
        Log.d(getClass().getSimpleName(), "onRecoverableError: " + errorMsg);

        return(true);
      } 

      private static void generateNotification(Context context, String message, String title ) 
      {
            int icon = R.drawable.ic_launcher;
            long when = System.currentTimeMillis(); // can change this to a future time if desired

            NotificationManager notificationManager = 
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            Intent notificationIntent = new Intent(context, ProblemioActivity.class);

            // set intent so it does not start a new activity
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);        
            Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);            

             Notification notification = new NotificationCompat.Builder(context)
             .setContentTitle(title)
             .setContentText(message)
             .setContentIntent(intent)
             .setSmallIcon(icon)
             .setLights(Color.YELLOW, 1, 2)
             .setAutoCancel(true)
             .setSound(defaultSound)
             .build();

            notificationManager.notify(0, notification);
    }     
}
碰撞发生在这条线上:

generateNotification(ctxt, extras.getString("message"), "New Message");
我是不是应该用这样的话来概括这句话:

if ( android.os.Build.VERSION.SDK_INT >= 8 )
{

}
在我的清单中,我允许所有sdk版本如下:

if ( android.os.Build.VERSION.SDK_INT >= 8 )
{

}

有一个支持库错误,请参阅。可以从中下载固定的v4支持库。

有一个支持库错误,请参阅。可以从中下载固定的v4支持库。

这是11版之前android-support-v4.jar中的一个已知错误

要获取最新的android-support-v4.jar,请执行以下操作:

  • 从IDE打开SDK管理器
  • 选中Android支持库,然后单击安装软件包
  • 导航到
    yourSdkFolder/extras/android/support/v4
    并复制
    android-support-v4.jar
  • 导航到项目中的
    android-support-v4.jar
    位置,然后粘贴,替换旧版本

  • 这是rev11之前android-support-v4.jar中的一个已知错误

    要获取最新的android-support-v4.jar,请执行以下操作:

  • 从IDE打开SDK管理器
  • 选中Android支持库,然后单击安装软件包
  • 导航到
    yourSdkFolder/extras/android/support/v4
    并复制
    android-support-v4.jar
  • 导航到项目中的
    android-support-v4.jar
    位置,然后粘贴,替换旧版本

  • 我应该把它放在一个try/catch语句中,这样它就不会使程序崩溃吗?试着根据bug报告页面构建一个固定的支持库jar。这样sdk 15下面的任何内容都不会在代码中显示通知?答案#5有点让我摸不着头脑。它可以在除13之外的任何API级别上工作。我添加了一个链接来下载修复库。我基本上把它包装在一个try/catch中…这是一个类似的解决方案吗?尝试{generateNotification(ctxt,extras.getString(“message”),“newmessage”);}catch(异常e){}我应该把它放在一个try/catch语句中,这样它就不会使程序崩溃吗?试着根据bug报告页面构建一个固定的支持库jar。这样sdk 15下面的任何内容都不会在代码中显示通知?答案#5有点让我摸不着头脑。它可以在除13之外的任何API级别上工作。我添加了一个链接来下载修复库。我基本上把它包装在一个try/catch中…这是一个类似的解决方案吗?尝试{generateNotification(ctxt,extras.getString(“message”),“newmessage”);}catch(异常e){}