-1

I have problem with setLatestEventInfo i can't change it to new version . please help me to rewrite new code.

    private void showNotification(int moodId, int textId) {
    CharSequence text = getText(textId);
    Notification notification = new Notification(R.drawable.icon,  getText(R.string.notify_message_started), System.currentTimeMillis());
    notification.setLatestEventInfo(this, getText(R.string.notify_message_running_headline), getText(R.string.notify_message_running_text),
            PendingIntent.getActivity(this, 0, new Intent(this, AudioMonitor.class).setFlags(603979776), 0));

    notification.flags |= 2;
   this.mNM.notify(MOOD_NOTIFICATIONS, notification);

}

I want can use flag for this line " notification.flags |= 2" in new level api. I am new beginner android

zahra
  • 3
  • 6
  • 1
    use [NotificationCompat.Builder](https://developer.android.com/reference/android/app/Notification.Builder.html) instead – ADM Jul 25 '17 at 14:09
  • 1
    Possible duplicate of [How to create a notification with NotificationCompat.Builder?](https://stackoverflow.com/questions/13902115/how-to-create-a-notification-with-notificationcompat-builder) – Tadija Bagarić Jul 25 '17 at 14:15
  • @Tadija Bagarić i want use flags in notification . this link dos'nt have this item – zahra Jul 25 '17 at 14:51

1 Answers1

0

You can perform a check for API level and then use the latest version

final int sdkVersion = Integer.parseInt(Build.VERSION.SDK);

if (sdkVersion < Build.VERSION_CODES.HONEYCOMB)
 {
   // Deprecated in API level 11
     CharSequence text = getText(textId);
     Notification notification = new Notification(R.drawable.icon,  
     getText(R.string.notify_message_started), System.currentTimeMillis());
     notification.setLatestEventInfo(this, getText(R.string.notify_message_running_headline), getText(R.string.notify_message_running_text),
        PendingIntent.getActivity(this, 0, new Intent(this, AudioMonitor.class).setFlags(603979776), 0));

     notification.flags |= 2;
     this.mNM.notify(MOOD_NOTIFICATIONS, notification);

 }
 else
 {
 // available from API level 11 and onwards
 final Intent notificationIntent = new Intent(this, AudioMonitor.class);
 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
 Intent.FLAG_ACTIVITY_SINGLE_TOP);
 PendingIntent contentIntent = PendingIntent.getActivity(myContext, 0, notificationIntent, 0);

 Notification notification = new Notification.Builder(mContext)
     .setContentTitle(getText(R.string.notify_message_running_headline))
     .setContentText(getText(R.string.notify_message_running_text))
     .setContentIntent(contentIntent);
     .setWhen(System.currentTimeMillis())
     .setOngoing(true)
     .setSmallIcon(R.drawable.icon)
     .setLargeIcon(R.drawable.icon)
     .build(); 
  NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(MOOD_NOTIFICATIONS, notification);
 } 
Kapil G
  • 4,081
  • 2
  • 20
  • 32
  • thank you but it's not complet . i want flag and System.currentTimeMillis()) and .. . According to my own codes . can you edit your code in "available from API level 11 and onwards" ? – zahra Jul 25 '17 at 14:38
  • You don't need a when if you want the current time but I have still added in the code. As for flags, you now have different methods for different flags so just numbers won't do. For now I have jsut added the ongoing one in the code as you were using FLAG INT 2. If you want to add more flags, you can look at their respective methods here. https://developer.android.com/reference/android/app/Notification.Builder.html – Kapil G Jul 25 '17 at 14:48
  • I don't need to use other parts of above code ? PendingIntent.getActivity(this, 0, new Intent(this, AudioMonitor.class).setFlags(603979776), 0)); and this.mNM.notify(MOOD_NOTIFICATIONS, notification); – zahra Jul 25 '17 at 14:57
  • You need to use them but that is the easy part to figure out :) You wanted to know how to convert to new API and that's what I have told. Do you want me to do these two for you as well? – Kapil G Jul 25 '17 at 15:06
  • yes i want full new api from my code :) i'm new beginner. thank you alot. i commented all of code // Deprecated in API level 11// because it will doesn't compile – zahra Jul 25 '17 at 15:15
  • @zahra update the code. Please accept the answer once you have verified it – Kapil G Jul 25 '17 at 15:48
  • this line has error : setContentIntent(notificationIntent) . in builder cann't be applied to (android.content.intent) – zahra Jul 25 '17 at 16:10
  • Sorry typo form my side. Change it to setContentIntent(contentIntent) – Kapil G Jul 25 '17 at 16:12