2014-10-28 102 views
2

我正在實施通知服務,並且遇到了在Android 2.3中收到消息的問題。我收到版本4.0和更新的消息,但不是2.3。在logcat中出現以下錯誤:在Android 2.3中未收到通知

找不到類com.google.android.gms.ads.internal.b.c,該方法引用自com.google.android.gms.ads.internal.nea方法 。 。 找不到類'android.app.Notification $ Builder',引用自com.google.android.gms.common.l.a方法

可能是什麼問題?這是我的方法發送通知:

private void sendNotification(String msg) { 

    MainActivity.notificationClicked = true; 

    int icon = R.drawable.ic_launcher; 
    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) this 
      .getSystemService(Context.NOTIFICATION_SERVICE); 

    String strMessage = loadPreferences(); 
    String newMessage = ""; 

    if (!strMessage.isEmpty()) 
     newMessage = strMessage + "<br>" + msg; 
    else 
     newMessage = msg; 

    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout); 
    contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher); 
    contentView.setTextViewText(R.id.text, Html.fromHtml(newMessage)); 


    Notification notification = new Notification(icon, Html.fromHtml(msg), when); 
    notification.contentView = contentView; 
    String title = this.getString(R.string.app_name); 


    ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 

    List <ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); 

    Log.d("current task :", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClass().getSimpleName()); 

    ComponentName componentInfo = taskInfo.get(0).topActivity; 

    Intent notificationIntent; 

    if(!componentInfo.getPackageName().equalsIgnoreCase("com.example.myapp")){ 
     notificationIntent = new Intent(getApplicationContext(), 
       FirstActivity.class); 
    } else { 
     notificationIntent = new Intent(getApplicationContext(), 
       MainActivity.class); 

     notificationIntent.putExtra("login", true); 
    } 
     notificationIntent.putExtra("message", Html.fromHtml(msg)); 

     oldMessage = newMessage; 


     savePreferences(getApplicationContext(), oldMessage); 

     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 

     int notifyID = 1; 

     PendingIntent intent = PendingIntent.getActivity(this, notifyID, 
       notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     notification.setLatestEventInfo(this, title, msg, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notification.contentIntent = intent; 

     notificationManager.notify(notifyID, notification); 

} 

當我發送消息我接收它在設備與Android 4.2例如,但不與Android 2.3的裝置。我正在用Android 2.3調試應用程序,並且我已經在IntentService的onHandleIntent方法中放置了一個斷點,但它永遠不會到來。我認爲這可能是logcat中出現的錯誤。

如何解決Android 2.3設備的問題?

在此先感謝。


更改通知到NotificationCompat:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); mBuilder.setContentIntent(intent); Notification notification = mBuilder.build(); notification.contentView = contentView;
notificationManager.notify(notifyID, notification);

但問題仍然存在,在logcat中出現同樣的錯誤

+0

可能應該使用支持庫來建立您的通知 – Blundell 2014-10-28 11:29:58

+0

您是否嘗試使用NotificationCompat類來獲得支持版本? – joao2fast4u 2014-10-28 11:31:22

回答

0

試試這個代碼。我在我的項目中使用了它,它運行良好。我記得,receiverActivity就是打開onclick的活動,但我不能100%確定它。

public static void showNotification (Context context, Class receiverActivityClass, String title, String text, int icon) 
    { 
     Intent intent = new Intent(context, receiverActivityClass); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 

     // Build notification 
     Notification notification = new Notification.Builder(context) 
       .setContentTitle(title) 
       .setContentText(text) 
       .setSmallIcon(icon) 
       .setContentIntent(pendingIntent) 
       .setAutoCancel(true) 
       .getNotification(); 


     NotificationManager notificationManager = 
       (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, notification); 
    }