Reputation: 459
I am following a code sample somewhere from internet in which i want to start a activity when click on notification. but when i click on show notification button it gives error.. Here is my notification code.
public void showNotification(){
// define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(MainActivity.this, BusinessDetailActivity.class);
intent.putExtra("id", 1);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// this is it, we'll build the notification!
// in the addAction method, if you don't want any icon, just set the first param to 0
Notification mNotification = new Notification.Builder(this)
.setContentTitle("New Post!")
.setContentText("Here's an awesome update for you!")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setSound(soundUri)
// .addAction(R.drawable.ic_launcher, "View", pIntent)
// .addAction(0, "Remind", pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotification.flags |=Notification.FLAG_AUTO_CANCEL;
// If you want to hide the notification after it was selected, do the code below
// myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, mNotification);
}
Here is my logcat.
06-28 23:32:29.150: E/AndroidRuntime(30461): FATAL EXCEPTION: main
06-28 23:32:29.150: E/AndroidRuntime(30461): java.lang.NoSuchMethodError: android.app.Notification$Builder.build
06-28 23:32:29.150: E/AndroidRuntime(30461): at com.faisalahsan.discountcloud.MainActivity.showNotification(MainActivity.java:130)
06-28 23:32:29.150: E/AndroidRuntime(30461): at com.faisalahsan.discountcloud.MainActivity.onMenuItemSelected(MainActivity.java:147)
06-28 23:32:29.150: E/AndroidRuntime(30461): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
06-28 23:32:29.150: E/AndroidRuntime(30461): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
06-28 23:32:29.150: E/AndroidRuntime(30461): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
06-28 23:32:29.150: E/AndroidRuntime(30461): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
06-28 23:32:29.150: E/AndroidRuntime(30461): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:490)
06-28 23:32:29.150: E/AndroidRuntime(30461): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:108)
06-28 23:32:29.150: E/AndroidRuntime(30461): at android.view.View.performClick(View.java:3519)
06-28 23:32:29.150: E/AndroidRuntime(30461): at android.view.View$PerformClick.run(View.java:14140)
06-28 23:32:29.150: E/AndroidRuntime(30461): at android.os.Handler.handleCallback(Handler.java:605)
06-28 23:32:29.150: E/AndroidRuntime(30461): at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 23:32:29.150: E/AndroidRuntime(30461): at android.os.Looper.loop(Looper.java:137)
06-28 23:32:29.150: E/AndroidRuntime(30461): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-28 23:32:29.150: E/AndroidRuntime(30461): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 23:32:29.150: E/AndroidRuntime(30461): at java.lang.reflect.Method.invoke(Method.java:511)
06-28 23:32:29.150: E/AndroidRuntime(30461): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
06-28 23:32:29.150: E/AndroidRuntime(30461): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
06-28 23:32:29.150: E/AndroidRuntime(30461): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 637
Reputation: 394026
If you wish to use similar code in older Android versions, you can use the android.support.v4.app.NotificationCompat.Builder
class, which is part of the compatibility library.
Notification mNotification = new NotificationCompat.Builder(this)
.setContentTitle("New Post!")
.setContentText("Here's an awesome update for you!")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setSound(soundUri)
// .addAction(R.drawable.ic_launcher, "View", pIntent)
// .addAction(0, "Remind", pIntent)
.build();
This would work in Android versions as old as API level 4.
Upvotes: 0
Reputation: 1210
.build() was introduced in API level 16. Use "getNotification()".
Upvotes: 1