2013-06-07 2 views
1

Я разрабатываю приложение, которое работает на API 7 и выше, поэтому я должен использовать NotificationCompat.Builder вместо уведомления, потому что он устарел в более высокой версии. Это отлично работает на эмуляторе, но при тестировании на моем устройстве не было notification. Пожалуйста, помогите мне. NB: Это не представляется возможным использовать только в API для API 7 до 14. Я хотел бы знать, потому что мое устройство использует API 7Уведомление о работе с эмулятором, но не на моем устройстве

ответ

2

попробовать эту функцию - она ​​работает на Android 2 до 4:

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 



    public static void pushNotification(final Context context, 
      int icon, String name, String descr, Intent activityIntent) { 
     NotificationManager notifyMgr = 
       (NotificationManager)context.getSystemService( 
       Context.NOTIFICATION_SERVICE); 
     long when = System.currentTimeMillis(); 
     PendingIntent pIntent = PendingIntent.getActivity(
       context, 0, activityIntent, 0); 
     Notification notification = null; 
     if (android.os.Build.VERSION.SDK_INT < 11) 
      notification = getNotification8(context, 
        icon, name, descr, when, pIntent); 
     else notification = getNotification11(context, 
       icon, name, descr, when, pIntent); 
     notifyMgr.notify(NOTIFY_ID, notification); 
    } 

    @SuppressWarnings("deprecation") 
    private static Notification getNotification8(Context context, 
      int icon, String name, String descr, 
      long when, PendingIntent pIntent) { 
     Notification notification = new Notification(icon, name, when); 
     notification.setLatestEventInfo(context, name, descr, pIntent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     return notification; 
    } 
    @TargetApi(11) 
    private static Notification getNotification11(Context context, 
      int icon, String name, String descr, 
      long when, PendingIntent pInten) { 
     Notification notification = new Notification.Builder(context) 
      .setTicker(name) 
      .setContentTitle(name) 
      .setContentText(descr) 
      .setSmallIcon(icon) 
      .setContentIntent(pInten) 
      .setAutoCancel(true) 
      .setWhen(when) 
      .getNotification();  
     return notification; 
    } 
+0

Спасибо Matreshkin, все, что мне нужно, это API, который работает для всех. –

Смежные вопросы