Notification not making sound or vibration? - java

I am making a program and I want the app to make a notification. When the notification goes off then only the ticker text is displayed. No sound or vibration or light is accompanied with it.
Here is an example of my code:
int icon = R.drawable.icon;
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Countdown Complete!";
Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new Notification(icon, myCountDown.getName() + " is completed!", System.currentTimeMillis());
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
notificationManager.notify(myCountDown.getId(), notification);

To add notification light you need to add this(notice the .flags, not .defaults):
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB=0xffffffff; //color, in this case, white
notification.ledOnMS=1000; //light on in milliseconds
notification.ledOffMS=4000; //light off in milliseconds
For default sound:
notification.defaults |= Notification.DEFAULT_SOUND;
For default vibration pattern:
notification.defaults |= Notification.DEFAULT_VIBRATE;
For vibration, as Dean Thomas mentioned, you'll need a permission <uses-permission android:name="android.permission.VIBRATE"/>

To make the LED work, you need to tell it what to do.
notification.ledOnMS = 1000; //Be on for a second
notification.ledOffMS = 1000; //Be off for a second
notification.ledARGB = Color.GREEN; //What colour should the LED be?
To make the vibrate work, you need permission added to your Manifest
<uses-permission android:name="android.permission.VIBRATE"/>

Did you ensure the device is not muted and actually has a notification sound picked by the user (also not silent)?
Another thing to try would be:
[Note obj].sound = value
[Note obj].LEDARGB = value
[Note obj].vibrate = value

I find there is a setDefaults method in NotificationCompat.Builder , and it provides you for adjusting notification preferences.
Here is an example:
NotificationManager notificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.taiwanpic)
.setWhen(System.currentTimeMillis())
.setContentTitle("AAAAA")
.setContentText("BBBBB")
.setSubText("CCCCC")
.setContentInfo("DDDDD")
.setTicker("EEEEE")
.setDefaults(Notification.DEFAULT_ALL);
int pid = android.os.Process.myPid();
notificationManager.notify(pid, builder.build());
If you don't use all default values, you can also try DEFAULT_SOUND, DEFAULT_VIBRATE,DEFAULT_LIGHTS as you want.

Related

How to send a google map notification for android auto simulator?

I'm developing an app for Android Auto. I wanted to send a map notification like this(I'm not implmented yet the big view)
I tried it with the following code. But it's failed to send a map navigation notification.
private void sendNotificationForConversation(Conversation conversation) {
// A pending Intent for reads
double latitude = 31.249351;
double longitude = 121.45905;
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://maps.google.com/maps?saddr="
+ latitude + ","
+ longitude + "&daddr="
+ 31.186371 + "," + 121.489885));
notificationManager = (NotificationManager) getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification_icon, "Kohls store ahead", 1000);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.activity_kcc);
contentView.setTextViewText(R.id.hello, "This is the KCC layout");
notification.contentView = contentView;
PendingIntent readPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = readPendingIntent;
notification.flags |= Notification.FLAG_AUTO_CANCEL; // clear the notification
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; // Sound
// Build a RemoteInput for receiving voice input in a Car Notification
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(getApplicationContext().getString(R.string.notification_reply))
.build();
// Building a Pending Intent for the reply action to trigger
PendingIntent replyIntent = PendingIntent.getBroadcast(getApplicationContext(),
conversation.getConversationId(),
getMessageReplyIntent(conversation.getConversationId()),
PendingIntent.FLAG_UPDATE_CURRENT);
// Create the UnreadConversation and populate it with the participant name,
// read and reply intents.
NotificationCompat.CarExtender.UnreadConversation.Builder unreadConvBuilder =
new NotificationCompat.CarExtender.UnreadConversation.Builder(conversation.getParticipantName())
.setLatestTimestamp(conversation.getTimestamp())
.setReadPendingIntent(readPendingIntent)
.setReplyAction(replyIntent, remoteInput);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setContent(contentView)
.setContentIntent(readPendingIntent)
.extend(new NotificationCompat.CarExtender()
.setUnreadConversation(unreadConvBuilder.build()));
MessageLogger.logMessage(getApplicationContext(), "Sending notification "
+ conversation.getConversationId() + " conversation: " + conversation);
notificationManager.notify(conversation.getConversationId(), builder.build());
}
with this I got an empty notification on simulator and there's nothing visible on status bar. I want to show a navigating map on android auto car device after click on this notification. How may I fix this?
Thanks in Advance!
I think you just can not do it now; There is no way you can launch the Android Auto's Google Maps activity directly. I tried to launch the Direction intent by:
Uri gmmIntentUri = Uri.parse("google.navigation:q=San+Francisco");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
mapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mapIntent);
but it does not seem to work.. I don't have the real head unit so I am not sure if this would work on the real device, but this is my best guess.

Android How to show alert on another app

Hi I want to show alert after click on my notification. Here is code:
#SuppressWarnings("deprecation")
private void Notify(String notificationTitle, String notificationMessage) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon_stat, "Powiadomionko", System.currentTimeMillis());
notification.ledARGB = Color.CYAN;//0xFFff0000;
notification.ledOnMS = 800;
notification.ledOffMS = 2400;
notification.vibrate = new long[]{100, 120, 100, 120};
Intent notificationIntent = new Intent(this, TerminarzActivity.class);
notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_VIBRATE;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(Serwis_updateTERM.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify(1, notification);
}
And I want to show Alert on anything not on my Activity after click. Anyone know how to do this ?
I know i must add
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
but nothing else..
Check out the creation of this notification (taken from Geofence sample). This code creates a notification and if you touch it it launches your MainActivity.
// Create an explicit content Intent that starts the main Activity
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
// Construct a task stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the main Activity to the task stack as the parent
stackBuilder.addParentStack(MainActivity.class);
// Push the content Intent onto the stack
stackBuilder.addNextIntent(notificationIntent);
// Get a PendingIntent containing the entire back stack
PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Get a notification builder that's compatible with platform versions
// >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Set the notification contents
builder.setSmallIcon(R.drawable.ic_folder)
.setContentTitle(getString(R.string.geofence_transition_notification_title, transitionType, ids))
.setContentText(getString(R.string.geofence_transition_notification_text))
.setContentIntent(notificationPendingIntent);
// Get an instance of the Notification manager
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(0, builder.build());
This code might help you, but still it depends what do you mean by "alert".

How can I open activity when notification is touched?

I am building a notification for my Music Player, so it displays what music is playing.
But I want to add a function where, when I touch it. It opens my layout player.xml. Which is deployed through the MainActivity.class.
I have researched on Notifications on developer.android.com and found a way to deploy and activity upon clicking the notification. But it did't work.
Here is my current code -
Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder.setContentTitle(songsList.get(songIndex).get("songTitle"));
builder.setContentText("NexPlay™");
builder.setTicker(songsList.get(songIndex).get("songTitle"));
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setSmallIcon(R.drawable.play_default);
builder.setAutoCancel(true);
builder.setPriority(0);
builder.setOngoing(true);
Notification notification = builder.build();
NotificationManager notificationManger =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManger.notify(01, notification);
Thanks to everyone for answering!
I added
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationToPlayer.class), 0);
builder.setContentIntent(pendingIntent);
to my notifications and it worked great !
Try this
private void showNotification() {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.service_started);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.android, text,
System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ServiceLauncher.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.service_label),
text, contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
// Send the notification.
// We use a layout id because it is a unique number. We use it later to cancel.
nm.notify(R.string.service_started, notification);
}
Hope this will solve your problem
You need to add intent on ur code:-
int icon = R.drawable.app_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message.split(":::")[1], when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = null;
notificationIntent = new Intent(context, MessageDetail.class);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, message.split(":::")[1], intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
You'll also want to add an intent. Something like:
Intent actIntent = new Intent(this, do MyActivity.class);
Then in your builder:
.setContentIntent(PendingIntent.getActivity(MyActivity, 131314, actIntent,
PendingIntent.FLAG_UPDATE_CURRENT))

Multiple push notifications on android with wrong icon size

Let me preface this post with the fact that I am new to the android programming.
When I issue a notification, the phone shows two notifications for the same thing. The only difference is, the icons look different and they do different things. The first will do nothing when you select it. The other opens the app to the correct page.
I used to only get one notification until I edited this method. I'm not sure what I did but perhaps someone could tell me why this sends 2 and how to fix it?
The other question I have is that on both notifications on the phone, the icon is zoomed in (or too big). How do you make the push notification icon the correct size?
Here is my method
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, FriendGroupActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
Try this :
private void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Notification notification;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, myactivity.class), 0);
// To support 2.3 os, we use "Notification" class and 3.0+ os will use
// "NotificationCompat.Builder" class.
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
notification = new Notification(icon, message, 0);
notification.setLatestEventInfo(context, appname, message,
contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(appname).setWhen(0)
.setAutoCancel(true).setContentTitle(appname)
.setContentText(message).build();
notificationManager.notify(0 , notification);
}
}
Hope this helps.This works for me.

Use notification ID to change logo & text

I'd like to know how applications like Facebook change their notification title & logo depending on the content.
For example, in Facebook, if you get tagged you get another title & another logo.
I assume it should be possible with notify to create a unique notification.
Though I can't find any clear examples for this.
My GenerateNotification:
private static void generateNotification(Context context, String message, String url) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, ShowChange.class);
notificationIntent.putExtra ("url",url);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
It should be something like this:(this is basically your code with some modifications)
public static void generateNotification(Context context, String message, String url,int icon_from_drawable) {
int icon = icon_from_drawable;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, ShowChange.class);
notificationIntent.putExtra ("url",url);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
If you targeting your app for API LEVEL>=11, then use Notification.Builder
refer here : http://developer.android.com/reference/android/app/Notification.Builder.html
One more point the above code will keep updating the same notification object because the notify method of notification manager is taking always the same id i.e 0, so the same object of notification will be updated.

Categories

Resources