2015-11-14 3 views
0

am using Androidexample.com psuh notificaion am new to android am get error to setLatestEventInfo, я нашел решение, что метод setLatestEventInfo() был удален из SDK в Android 6.0. пожалуйста, помогите ниже кодsetLatestEventInfo Не удается решить в api 23+

частный статический недействительным generateNotification (контекст контекста, строка сообщений) {

int icon = R.drawable.ic_launcher; 
    long when = System.currentTimeMillis(); 

    NotificationManager notificationManager = (NotificationManager) 
      context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 

    String title = context.getString(R.string.app_name); 

    Intent notificationIntent = new Intent(context, MainActivity.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); 

    notification.setLatestEventInfo(context, title, message, intent); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    // Play default notification sound 
    notification.defaults |= Notification.DEFAULT_SOUND; 

    notification.sound = Uri.parse(
    "android.resource://" 
      + context.getPackageName() 
      + "your_sound_file_name.mp3"); 

    // Vibrate if vibrate is enabled 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notificationManager.notify(0, notification); 

} 

ответ

2

Согласно: https://developer.android.com/sdk/api_diff/23/changes/android.app.Notification.html

Этот метод был удален в M (API 23). Поэтому, если ваша версия SDK для компиляции установлена ​​на api 23+, вы увидите эту проблему.

Thereofore попробовать это:

private void raiseNotification(String mimeType, File output, 
           Exception e) { 
    NotificationCompat.Builder b=new NotificationCompat.Builder(this); 

    b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL); 

    if (e == null) { 
     b.setContentTitle(getString(R.string.download_complete)) 
     .setContentText(getString(R.string.fun)) 
     .setSmallIcon(android.R.drawable.stat_sys_download_done) 
     .setTicker(getString(R.string.download_complete)); 

     Intent outbound=new Intent(Intent.ACTION_VIEW); 

     outbound.setDataAndType(Uri.fromFile(output), mimeType); 

     b.setContentIntent(PendingIntent.getActivity(this, 0, outbound, 0)); 
    } 
    else { 
     b.setContentTitle(getString(R.string.exception)) 
     .setContentText(e.getMessage()) 
     .setSmallIcon(android.R.drawable.stat_notify_error) 
     .setTicker(getString(R.string.exception)); 
    } 

    NotificationManager mgr= 
     (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

    mgr.notify(NOTIFY_ID, b.build()); 
    } 
} 
+0

спасибо за быстрый ответ, пожалуйста, вы можете предложить исправление или изменения в моем выше коде –

+0

использования NotificationCompat.Builder. http://developer.android.com/reference/android/app/Notification.Builder.html – Androider

1

Через час & час работы я пришел к решению ... Я так счастлив сегодня !!! если compleate код какой-либо одной ХОЧЕТ для GCM нажимного уведомления вы можете по электронной почте мне [email protected]

private static void generateNotification(Context context, String message) { 

     int icon = R.drawable.ic_launcher; 
     long when = System.currentTimeMillis(); 

     Intent intent = new Intent(context, MainActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder b = new NotificationCompat.Builder(context); 

     b.setAutoCancel(true) 
       .setDefaults(Notification.DEFAULT_ALL) 
       .setWhen(System.currentTimeMillis()) 
       .setSmallIcon(icon) 
       .setTicker("Hearty365") 
       .setContentTitle("Default notification") 
       .setContentText(message) 
       .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND) 
       .setContentIntent(contentIntent) 
       .setContentInfo("Info"); 


     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(1, b.build()); 

    }