I am getting errors with these lines while handling notifications for different API levels. This is how i did so far:
...
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Notification notification;
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB){
notification = new Notification(icon, text, time);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
}
else
{
notification = new Notification.Builder(this) // error
.setContentTitle(title) // in
.setContentText(tmp_task_brief) // these
.setSmallIcon(icon) // lines
.setLargeIcon(null) // telling "this method call requires API level 11
.build(); // or higher"
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
}
...
I don't understand how to remove these errors. Please help me.
Edit: I did applied edit as below but NotificationCompact.Builer too got deprecated method getNotification() that returns Notification object.
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB)
{
notification = new Notification(icon, text, time);
notification.setLatestEventInfo(this, title, text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
}
else
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(contentIntent)
.setSmallIcon(icon)
.setTicker(text)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(text);
notification = builder.getNotification();
mNM.notify(NOTIFICATION, notification);
}
Use NotificationCompact.Bulider from support liberary (V4 liberary) that supports from 1.6
i think that will solves your problem.
Finally, with the help of these guys i ended up to the solution of handling the deprecated methods:
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
notification = new Notification(icon, text, time);
notification.setLatestEventInfo(this, title, text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(text).setWhen(time)
.setAutoCancel(true).setContentTitle(title)
.setContentText(text).build();
mNM.notify(NOTIFICATION, notification);
}
Related
I simply call a Notification . But It continuously playing sound unless I drag notification ..
Below is my code and right now I am testing on below OREO..
NotificationManager mNotificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "droidudes.zcode";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String name = "easyTouch";
String Description = "Best Utility Tool";
int importance = NotificationManager.IMPORTANCE_HIGH;//IMPORTANCE_HIGH
NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern();
mChannel.setShowBadge(false);
mNotificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext, NOTIFICATION_CHANNEL_ID)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
///.setVibrate(longArrayOf(0, 1000, 500, 1000))
.setSmallIcon(R.drawable.icon_of_app)
.setContentTitle(mContext.getString(R.string.app_name))
.setContentText("Toucher Hiding Here")
.setAutoCancel(true);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 1,
new Intent(mContext,EasyTouchService.class)
.setAction(EasyTouchService.ACTION_SHOW), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
Notification mNotification = mBuilder.build();
mNotification.flags |= Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
//mNotification.tickerText = getString(R.string.app_name);
// setUpAsForeground(message);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
I have tried multiple ways by commenting vibration sound in above code . But still sound play for unlimited time..
Try this out:
final PendingIntent pendingIntent = PendingIntent.getActivity(
mCtx,
notfication_id,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(mCtx);
Notification mNotif = builder.setSmallIcon(R.mipmap.logo)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentTitle("title of your notification")
.setContentText("details of your notification")
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(),R.drawable.logo_full))
.build();
mNotif.flags = Notification.FLAG_AUTO_CANCEL;
mNotif.defaults |= Notification.DEFAULT_SOUND;
mNotif.defaults |= Notification.DEFAULT_VIBRATE;
NotificationManager notificationManager = (NotificationManager)mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), mNotif);
I'm creating an Android application. What I'm looking for is after computing the results I want the numeric value to be displayed on the notification window. How can I do it?
To display your result into notification you can use below code to create notification:
Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
R.drawable.noti_icon);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.noti_icon)
.setLargeIcon(icon)
.setContentTitle("Your Title of Notification")
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setContentText("write here your score which you have counted");
Intent notificationIntent = new Intent(this, YourActivity.class); // here on notification click you are moved on this activity
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(contentIntent);
Notification notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.visibility |= Notification.VISIBILITY_PUBLIC;
notificationId = 0;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notification);
I didn't find a single solution on the internet that solves the problem.
int notificationId = Integer.parseInt(Common.getStringSharedPreference(context, "NOTIFICATION_ID", "ID", "1"));
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.app_logo, message, System.currentTimeMillis());
String title = context.getString(R.string.app_name);
Intent notificationIntent = null;
notificationIntent = new Intent(context, MyLawyerActivity.class);
notificationIntent.putExtra(ExtrasKeys.ITEM_OBJECT, (Serializable) object);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, notificationId, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.sound = Settings.System.DEFAULT_NOTIFICATION_URI;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
Common.putStringSharedPreference(context, "NOTIFICATION_ID", "ID", ""+(notificationId+1));
Note that I am using a device with lollipop for testing.
Just change the number for multiple notification like
notificationManager.notify(0, notification);
notificationManager.notify(1, notification);
I reasearched this, and this code is what I got
The app crashes when I open the MainActivity
NotificationCompat.Builder builder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Get Other APW Co. Apps on Play!")
.setContentText("Want more? All our apps are free!");
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) getSystemmService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, builder.build());
Try this, Give context name getSystemmService(Context.NOTIFICATION_SERVICE);.
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_launcher,
"Notification!", System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "App Name";
String notificationText = Msg;
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog),
context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,
0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context, notificationTitle,
notificationText, pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
This will work:
notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_launcher,
"Notification!",
System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "Get Other APW Co. Apps on Play!";
String notificationText = ""Want more? All our apps are free!"";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(SOME_URL));
PendingIntent pendingIntent
= PendingIntent.getActivity(AndroidNotification.this,
0, myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context,
notificationTitle,
notificationText,
pendingIntent);
notificationManager.notify(1, myNotification);
}});
You have to specify contentIntent, i.e. the PendingIntent that will be executed when the item is clicked. It's mandatory, and you haven't specified it which causes the error.
You can do this in the builder or on the Notification:
in builder:
builder.setContentIntent(contentIntent);
Notification n = builder.build();
on Notification:
Notification n = builder.build();
n.contentIntent = contentIntent;
Only after can you send it to the NotificationManager:
mNotifyMgr.notify(mNotificationId, n);
The exact contentIntent value depends on what you want to do. See reference here:
http://developer.android.com/reference/android/app/Notification.html#contentIntent
See working example here: https://github.com/nheid/unitedcoders-android/blob/master/src/com/unitedcoders/android/examples/download/DownloadProgress.java
I have the Problem, when a user press the Notification activity is opens but it can't be closed.
Here is my code:
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Good Morning";
long time = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, time);
Context context = getApplicationContext();
CharSequence contentTitle = "Title";
CharSequence contentText = "Text";
Intent notificationIntent = new Intent(this, NotificationManagerDemoActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,0);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1,notification);
try it like this..hope it will work..
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Set Auto Cancel to true while building Notification as follow.
notificationBuilder.setAutoCancel(true);
You should try to write code like below
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext(), "Channel")
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher_logo_round))
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher_logo_round);
mNotificationManager.notify(id,mBuilder.build());
.setAutoCancel(true)