I am working on an app in android in which i wish to notify specific users about specific events. These notifications are made by using the the Firebase Cloud Messaging data-messages. When I send a data-message to the users client, the onMessageReceived-method is called and I can handle these messages as I wish.
Now I want to make the device vibrate in the very moment the messages arrives. Therefore, I tried to build a notification and set the vibration on it, however it does not vibrate at all..
I included the VIBRATE-permission in my applications Manifest as well.
Here is my Code to build the notification:
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_icon)
.setContentTitle("Content Title")
.setContentText("Content Text")
.setVibrate(new long[] {500,500,500,500,500,500,500,500,500});
Notification note = mBuilder.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, note);
Have I missed anything?
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic)
.setContentTitle(context.getString(R.string.text))
.setContentText(context.getString(R.string.app_name))
.setContentIntent(contentIntent);
mBuilder.setVibrate(new long[]{500, 500});
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotifyMgr.notify(001, mBuilder.build());
This works for me.
Related
My app has a certain functionality in which I need to set device to silent mode without vibration. This is triggered by the arrival of a notification.
I used the following code to do this:
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
This works when the device screen is ON, but does not work many times, when the device screen is OFF (when it is left idle) for some time.
Is there any way to make this work everytime, even when the phone screen is OFF?
Try this
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this);
this will lead to a pattern of vibrate use the notification Builder and remove the set vibrate, this will disable the vibration.
Example of handling the sound and vibrate:
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(body)
.setContentTitle(title)
.setSound(soundUri)
.setLargeIcon(icon)
.setSound(soundUri)
.setLights(Color.parseColor("#ffb400"), 50, 10)
.setVibrate(new long[]{500, 500, 500, 500})
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
I am implementing FCM to push notifications, but I am trying to expand the notification when the content of the notification is big.
It cannot be implemented when the app is in background
here is my code
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_new_logo).setTicker(title).setWhen(0)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_stat_new_logo))
.setContentTitle("SuezApp")
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))
.setPriority(Notification.PRIORITY_MAX)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
final NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle(notificationBuilder);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
How can I expand it when the app is in background?
Per the Firebase 9.4 release notes:
Expanded gestures are now supported for messages, allowing the Android UI to display multiple lines when the body of a notification exceeds a single line.
Make sure you are using Firebase 9.4 or higher as your dependency.
I want to show notifications like on picture. If there is more than one, I want to show a counter too. I didn't find info in official doc. Now I just update my notification by id:
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
.notify(PUSH_NOTIFICATION_ID, notification);
How can I do it ?
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext());
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle(topic);
mBuilder.setContentText(new String(message.getPayload()));
mBuilder.setAutoCancel(true);
mBuilder.mNumber = 1;//get your number of notification from where you have save notification
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(notify_id, mBuilder.build());
To create a stack, call setGroup() for each notification you want in the stack and specify a group key.
final static String GROUP_KEY_EMAILS = "group_key_emails";
// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(mContext)
.setContentTitle("New mail from " + sender1)
.setContentText(subject1)
.setSmallIcon(R.drawable.new_mail)
.setGroup(GROUP_KEY_EMAILS)
.build();
// Issue the notification
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(notificationId1, notif);
Reference: https://developer.android.com/training/wearables/notifications/stacks.html
I'm programming an android app, which shows a notification. I know how to show it but how can my app learn whether it is removed by being swiped right or left? Is there any way to know this?
This is the way I created the notification:
public void showNotification(){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("My notification");
mBuilder.setContentText("Hello World!");
Notification mNotification = mBuilder.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(R.id.note, mNotification);
}
I have got this simple code that issues a Notification.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(
R.drawable.ic_launcher)
.setContentTitle("Error deactivating Tracker")
.setContentText("Unable to send deactivation SMS");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
The notifications shows correctly on a 4.4.2 device, but it does not show on a 2.3.6 device.
I am using the NotificationCompat so I suppose it should show.
What am I doing wrong?
Solved.
Apparently it wants a PendingIntent in any case,
so I added it:
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(
R.drawable.ic_launcher)
.setContentTitle("Error deactivating Tracker")
.setContentText("Unable to send deactivation SMS")
.setContentIntent(pi).setTicker("Error deactivating Tracker");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());