How to play sound file when GCMIntentService onMessage fires? - android

In my application I am using GCM services. I want to beep or play a small mp3 file at onMessage function.
My GCMIntentService is extended from GCMBaseIntentService.
Can somebody please share the code or suggest me how to achieve this.
Thanks

You could use:
Notification notification = new Notification(icon, title, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND; // To play default notification sound
notification.sound = Uri.parse("PATH_TO_YOUR_SOUND_FILE"); // to play custom notification sound
NotificationManager nm = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(System.currentTimeMillis(), notification);

Related

Can't keep notification sound playing after notification drawer is opened

I'm trying to make an alarm app, and I'm really close, but I want the alarm sound to keep playing until the user has manually disabled it via my app (puzzle to turn off alarm type idea). The problem Im having now is that the sound plays up until I pull down the notification drawer, then it stops.
Here is the relevant code:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
notificationBuilder.setContentTitle(alarm.getLabel());
notificationBuilder.setContentText(alarm.getMessage());
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
notificationBuilder.setSound(alarmSound);
notificationBuilder.setOngoing(true);
Notification notification = notificationBuilder.build();
if(alarm.getVibrate()) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_INSISTENT;
notificationManager.notify(2, notification);
I have tried messing around with the flags, but nothing seems to keep it playing...
Thanks for any help in advance.
Found a solution. Had to use a background Service to play the ringtone rather than the notification manager.

clearing ongoing notification in android

In my application iam showing a ongoing notification while doing a video upload. I want to clear the notification after upload. I searched a lot and cant get a clear answer.Please help me if anybody knows...
From Managing your Notifications in android documentation:
You can also clear it manually with cancel(int), passing it the
notification ID, or clear all your notifications with cancelAll().
So you should perform following to hide the notification:
mNotificationManager.cancel(notification_id);
where mNotificationManager is NotificationManager object and notification_id is int identifier of notification passed to NotificationManager object:
mNotificationManager.notify(notification_id, notification);
Just to show how this is done with Notification.Builder you can set the FLAG_AUTO_CANCEL like this:
Notification.Builder mBuilder = new Notification.Builder(getContext())
.setSmallIcon(R.drawable.icon)
.setContentTitle("My notification")
.setContentText("Hello Notify!")
.setOngoing(true)
.setAutoCancel(true);
You can clear your notification by clicking on it..!!
Notification notification = new Notification("Your needed data..");
notification.flags |= Notification.FLAG_AUTO_CANCEL;

How to use notification with sound and vibration?

How do you allow the device to vibrate or make a sound when a notification is launched.
Ive heard of the FLAGS. But how would i use them for Sound and Vibration?
Edit In Android v4 support library, there is NotificationCompat.Builder and the method setSound which will work if using that class instead. However, the info below will still work.
Notification notif ... //create your notification
notif.defaults |= Notification.DEFAULT_SOUND;
notif.defaults |= Notification.DEFAULT_VIBRATE;
That adds sound and vibrate.
Always remember to check the docs. They have a LOT of answers, such as this one:
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
That has the info I posted above as well as info on how to use a custom sound or vibrate and also the entire process of creating a notification.
EDIT: Don't forget to include the Vibrate permission in your manifest.
Notification notification // new notification object.
notification.defaults = Notification.DEFAULT_ALL;
Shows all the default notifications. (sound, vibration, led)

make android notification stay until user clears it

My question is regarding Notification and NotificationManager,
in my app I use a Notification to alert the user, but it plays the mp3 I gave it only once, and vibrates only ones, I wish it to play a sound and vibrate until the user clear the Notification by pressing clear. I couldn't find an example that simplified it, what's the best way to achieve that?
Thank All that reply,
Amitos80
there are flags you can set for achieving desired result,
Notification notification = new Notification(mIcon, mTickerText, mWhen);
notification.setLatestEventInfo(mContext, mContentTitle, mContentText, mContentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags = Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(ID, notification);
This will work definitely.
If you find it useful dont forget to mark it as an answer.

Android: How to create an "Ongoing" notification?

Hello, How do i create the permanent notification like the first one for Battery Indicator?
In case you are using NotificationCompat.Builder , you can use :
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
mBuilder.setOngoing(true); //this will make ongoing notification
Assign Notification.FLAG_ONGOING_EVENT flag to your Notification.
Sample code:
yourNotification.flags = Notification.FLAG_ONGOING_EVENT;
// Notify...
If you aren't familiar with the Notification API, read Creating Status Bar Notifications on Android developers website.
It is actually recommended to bitwise-or notification flags, rather than set the flags directly. This allows you to set multiple flags at once.
For example:
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
will set both flags at once, whereas:
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.flags = Notification.FLAG_SHOW_LIGHTS;
will only set the FLAG_SHOW_LIGHTS flag.
public static final int FLAG_ONGOING_EVENT
Since: API Level 1
Bit to be bitwise-ored into the flags field that should be set if this notification is in reference to something that is ongoing, like a phone call.
public static final int FLAG_FOREGROUND_SERVICE
Since: API Level 5
Bit to be bitwise-ored into the flags field that should be set if this notification represents a currently running service.

Categories

Resources