How to display results in notification bar on Android - android

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);

Related

Notification sound not stopping android

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);

push Notification in android with android studio

I do the next for call a Notification on android studio
when I send a message out of the app, only show the Icon and the text, but does not play sound or vibration
when I am inside the app, play sound and vibrate.
Any help?
I have on manifest
Method for show notification:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
//Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Uri defaultSoundUri = Uri.parse("android.resource://"+this.getPackageName()+"/"+R.raw.power);
//Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification.Builder notificationBuilder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.colantaico)
.setContentTitle(Titulo)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setLights(Color.RED, 3000, 3000)
.setContentIntent(pendingIntent);
Notification mNotification = notificationBuilder.build();
mNotification.flags |= Notification.FLAG_INSISTENT;
mNotification.sound = defaultSoundUri;
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, mNotification);
In the Manifest
<uses-permission android:name="android.permission.VIBRATE" />
<service
android:name=".RecibidorNotificacionesFCM">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
private static final int NOTIFICATION_ID = 1;
Intent openIntent = new Intent(this, MainActivity.class);;
PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
openIntent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setDefaults(Notification.DEFAULT_ALL)
.setVibrate(new long[]{100, 250, 100, 250, 100, 250})
.setAutoCancel(true)
.setColor(this.getResources().getColor(R.color.activity_toolbar_color))
.setContentTitle("Test Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(Html.fromHtml("Notification text")))
.setPriority(Notification.PRIORITY_MAX)
.setContentText(Html.fromHtml("Notification text")))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.notification_icon1);
} else {
mBuilder.setSmallIcon(R.drawable.notification_icon);
}
mBuilder.setContentIntent(contentIntent);
mBuilder.setDeleteIntent(LocalNotificationEventReceiver.getDeleteIntent(this));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = mBuilder.build();
notificationManager.notify(NOTIFICATION_ID, notification);

Activity not opens on notification clicked

I am new to android programming please help me out what I am missing.
I receive notification but my messageActivity is not opened
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("Message from " + name)
.setContentText(message)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setSound(soundUri);
if (strVibratePreference)
mbuilder.setVibrate(new long[]{100, 100});
notificationManager.notify(110, mbuilder.build());
Intent intent=new Intent(MessageService.this,MessageActivity.class);
intent.putExtra("name",name);
intent.putExtra("phno",_from);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(MessageService.this,0,intent,0);
mbuilder.setContentIntent(pendingIntent);
add this end code
Notification n = mbuilder.build();
notificationManager.notify(YOUR_NOTIF_ID, n);
Intent intent = new Intent(MessageService.this, MessageActivity.class);
intent.putExtra("name",name);
intent.putExtra("phno",_from);
PendingIntent pIntent = PendingIntent.getActivity(MessageService.this, (int) System.currentTimeMillis(), intent, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(MessageService.this)
.setContentTitle("New mail from " + "test#gmail.com")
.setContentText("Subject")
.setContentIntent(pIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);

Vibrate and Sound defaults on notification

I'm trying to get a default vibrate and sound alert when my notification comes in, but so far no luck. I imagine it's something to do with the way I set the defaults, but I'm unsure of how to fix it. Any thoughts?
public void connectedNotify() {
Integer mId = 0;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notify)
.setContentTitle("Device Connected")
.setContentText("Click to monitor");
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(getApplicationContext(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setOngoing(true);
Notification note = mBuilder.build();
note.defaults |= Notification.DEFAULT_VIBRATE;
note.defaults |= Notification.DEFAULT_SOUND;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, note);
}
Some dummy code might help you.
private static NotificationCompat.Builder buildNotificationCommon(Context _context, .....) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(_context)
.setWhen(System.currentTimeMillis()).......;
//Vibration
builder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
//LED
builder.setLights(Color.RED, 3000, 3000);
//Ton
builder.setSound(Uri.parse("uri://sadfasdfasdf.mp3"));
return builder;
}
Add below permission for Vibration in AndroidManifest.xml file
<uses-permission android:name="android.permission.VIBRATE" />
An extension to TeeTracker's answer,
to get the default notification sound you can do as follows
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notify)
.setContentTitle("Device Connected")
.setContentText("Click to monitor");
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
This will give you the default notification sound.
Notification
Vibrate
mBuilder.setVibrate(new long[] { 1000, 1000});
Sound
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
for more sound option
Its work fine to me,You can try it.
protected void displayNotification() {
Log.i("Start", "notification");
// Invoking the default notification service //
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this);
mBuilder.setAutoCancel(true);
mBuilder.setContentTitle("New Message");
mBuilder.setContentText("You have "+unMber_unRead_sms +" new message.");
mBuilder.setTicker("New message from PayMe..");
mBuilder.setSmallIcon(R.drawable.icon2);
// Increase notification number every time a new notification arrives //
mBuilder.setNumber(unMber_unRead_sms);
// Creates an explicit intent for an Activity in your app //
Intent resultIntent = new Intent(this, FreesmsLog.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(FreesmsLog.class);
// Adds the Intent that starts the Activity to the top of the stack //
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
// mBuilder.setOngoing(true);
Notification note = mBuilder.build();
note.defaults |= Notification.DEFAULT_VIBRATE;
note.defaults |= Notification.DEFAULT_SOUND;
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on. //
mNotificationManager.notify(notificationID, mBuilder.build());
}
This is a simple way to call notification by using default vibrate and sound from system.
private void sendNotification(String message, String tick, String title, boolean sound, boolean vibrate, int iconID) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Notification notification = new Notification();
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
if (sound) {
notification.defaults |= Notification.DEFAULT_SOUND;
}
if (vibrate) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
notificationBuilder.setDefaults(notification.defaults);
notificationBuilder.setSmallIcon(iconID)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setTicker(tick)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Add vibrate permission if you are going to use it:
<uses-permission android:name="android.permission.VIBRATE"/>
Good luck,'.
I m using the followung code and its working fine for me .
private void sendNotification(String msg) {
Log.d(TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("GCM Notification")
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d(TAG, "Notification sent successfully.");
}
To support SDK version >= 26, you also should build NotificationChanel and set a vibration pattern and sound there. There is a Kotlin code sample:
val vibrationPattern = longArrayOf(500)
val soundUri = "<your sound uri>"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val attr = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build()
val channelName: CharSequence = Constants.NOTIFICATION_CHANNEL_NAME
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel =
NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID, channelName, importance)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.enableVibration(true)
notificationChannel.setSound(soundUri, attr)
notificationChannel.vibrationPattern = vibrationPattern
notificationManager.createNotificationChannel(notificationChannel)
}
And this is the builder:
with(NotificationCompat.Builder(applicationContext, Constants.NOTIFICATION_CHANNEL_ID)) {
setContentTitle("Some title")
setContentText("Some content")
setSmallIcon(R.drawable.ic_logo)
setAutoCancel(true)
setVibrate(vibrationPattern)
setSound(soundUri)
setDefaults(Notification.DEFAULT_VIBRATE)
setContentIntent(
// this is an extension function of context you should build
// your own pending intent and place it here
createNotificationPendingIntent(
Intent(applicationContext, target).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
)
)
return build()
}
Be sure your AudioAttributes are chosen right to read more here.
// set notification audio
builder.setDefaults(Notification.DEFAULT_VIBRATE);
//OR
builder.setDefaults(Notification.DEFAULT_SOUND);
For Kotlin you can Try this.
var builder = NotificationCompat.Builder(this,CHANNEL_ID)
.setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)

MainActivity is Crashing With Status Bar 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

Categories

Resources