I am using following code to get notifications from a service
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this,test.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,Notification.FLAG_AUTO_CANCEL);
String body = "Hi this is test"+count;
String title = "Ritu"+count;
Notification n = new Notification(R.drawable.icon,body,System.currentTimeMillis());
n.setLatestEventInfo(this, title, body, pi);
n.defaults = Notification.DEFAULT_LIGHTS;
n.number=++count;
nm.notify(uniqueid, n);
Every time I am getting a new notification, the number on it gets incremented but when I open notification window I can see only one the latest notification and after clicking on it number of notification does not get decremented.
Where I am wrong please help.
Thanks!
Try this, in order to remove notification after click:
n.flags |= Notification.FLAG_AUTO_CANCEL;
And instead of using:
n.number = ++count;
Use:
count++;
nm.notify(count, n);
This will allow you to have multiple notifications at the same time
Follow this like if u can get help...
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/NotifyWithText.html
Related
I'm not getting sound, vibration or light when receiving a notification. Can someone please tell me what I'm missing?
Also I have some other questions:
1.) I only get the specified icon when the app is open. When the app is closed I get the android logo icon (I guess thats because I havent defined an app icon yet).
2.) The ticker text is only shown when the app is open.
3.) When the app is open I dont get the content text, only the content title.
SOLUTION: This happens because I am using com.google.android.gms:play-services-gcm:8.4.0 over the previous version.
Make sure that on the server you send a notification array containing only the key/value pair e=0 while sending your message information in the data array.
This problem has already a great answer here: After updating Google play services to 8.4.0 push notifications displayed by themselves
This is my Source:
public class MyGcmListenerService extends GcmListenerService {
private final static String TAG = "GCM_Listener";
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from + " (" + message);
// Sets an ID for the notification
SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.sharedPreferenceStore_default), Context.MODE_PRIVATE);
int mNotificationId = sharedPreferences.getInt("id_notification", 0);
mNotificationId++;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.all_picks_made_indicator)
.setAutoCancel(true)
.setContentTitle("Product - " + mNotificationId)
.setContentText(message)
.setTicker("Product Notification received");
// Because clicking the notification opens a new ("special") activity, there's no need to create an artificial back stack.
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, DetectLoginActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
Notification notification = mBuilder.build();
notification.defaults = Notification.DEFAULT_ALL;
mNotifyMgr.notify(mNotificationId, notification);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("id_notification", mNotificationId);
editor.commit();
}
}
Custom Sound for Push Notification
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pop);
In your code change
Notification notification = mBuilder.build();
notification.defaults = Notification.DEFAULT_ALL;
mNotifyMgr.notify(mNotificationId, notification);
To.
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.pop);
notification.defaults |= Notification.DEFAULT_VIBRATE;
mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
mBuilder.setLights(getResources().getColor(R.color.mainColor), 1000, 1000);
you can set color and vibrate on the builder object
You're missing something like this:
long[] pattern = new long[]{0, 100, 200, 100}; //two short beeps
mBuilder.setVibrate(pattern);
Notification note = mBuilder.build();
note.vibrate = pattern;
That will give you vibrations. Look into lights, I don't have that code at hand atm.
Months ago I wrote a code that my Notification worked as bellow :
NotificationManager NotiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String MyText = "NotificationText";
Notification mNotification = new Notification(R.mipmap.icon, MyText, System.currentTimeMillis() );
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotification.defaults |= Notification.DEFAULT_SOUND;
mNotification.defaults |= Notification.DEFAULT_VIBRATE;
String MyNotificationTitle = "AnyText";
String MyNotificationText = "HERE MORE TEXT";
Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("https://www.google.com"));
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent StartIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
mNotification.setLatestEventInfo(context.getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
NotiManager.notify(NOTIFY_ME_ID_LOGIN, mNotification);
But now, that I want to make an update it doesn't even let compile the APP because this line :
mNotification.setLatestEventInfo(context.getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
Is there any way to change the setLatestEventInfo or other way to create a Notification?
Is there any way to change the setLatestEventInfo
You are welcome to lower your compileSdkVersion, though that will introduce its own set of issues.
or other way to create a Notification?
As Blackbelt notes in a comment, NotificationCompat.Builder has been around for ~4 years and is the recommended way to create Notification objects today:
private void raiseNotification(String mimeType, File output,
Exception e) {
NotificationCompat.Builder b=new NotificationCompat.Builder(this);
b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);
if (e == null) {
b.setContentTitle(getString(R.string.download_complete))
.setContentText(getString(R.string.fun))
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setTicker(getString(R.string.download_complete));
Intent outbound=new Intent(Intent.ACTION_VIEW);
outbound.setDataAndType(Uri.fromFile(output), mimeType);
b.setContentIntent(PendingIntent.getActivity(this, 0, outbound, 0));
}
else {
b.setContentTitle(getString(R.string.exception))
.setContentText(e.getMessage())
.setSmallIcon(android.R.drawable.stat_notify_error)
.setTicker(getString(R.string.exception));
}
NotificationManager mgr=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mgr.notify(NOTIFY_ID, b.build());
}
(from this sample project, which is in this directory of sample projects all showing how to display Notifications)
i have an android program, in which i receive a message from the database at the external server on web then i put message in SMS inbox.
now i use system notification like this :
Notification intent :
ctx = context ;
notificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
syncNotification = new Notification();
notificationIntent = new Intent(Intent.ACTION_MAIN,null);
notificationIntent.setComponent(new ComponentName("com.android.mms","com.android.mms.ui.ConversationList"));
notificationIntent.addFlags(Notification.FLAG_AUTO_CANCEL);
contentIntent = PendingIntent.getActivity(ctx, 0,
notificationIntent, 0);
Notification creation :
syncNotification.icon = android.R.drawable.stat_notify_chat;
syncNotification.tickerText = ctx.getText(R.string.new_message);
syncNotification.when = System.currentTimeMillis();
syncNotification.setLatestEventInfo(ctx, ctx.getText(R.string.new_message), ctx.getText(R.string.check_your_inbox),contentIntent);
notificationManager.notify(5, syncNotification);
and then play sms ringtone:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(ctx, notification);
r.play();
after receive message and put it into inbox, notification shown but when i tap it nothing done.
are there a way that after inserting message to inbox, device show default notification and manage it ?
in android api 14 and later it is not possible because google prevent .
Right now I am using 3 devices for testing: Samsung S7500, Galaxy Mexus, HTC explore. If the devices are active, then I am receiving push notification in all three devices. If it is in idle state(locked/sleep), then also I am receiving push notification in all three devices. But if it is offline when the push message has been send, the time I am getting online should receive all pending notifications as per the documentation. But in this case I am not receiving notification in all the devices. Specially in Galaxy Nexus, sometime I am receiving if it is in idle state and sometime not. Where I am getting wrong? Can anyone help me?
String posted to GCM server from Third party server(.Net) end: where "i" is the current time.
String postdata= "collapse_key=score_update"+i+"&time_to_live=2419200&delay_while_idle=1&data.message="+ message + "&data.time=" + System.DateTime.Now.ToString()
+ "®istration_id=" + deviceToken + "";
Mentioned all required permissions in manifest as per documentation including Wakelock. This is my code for notiication in GCMIntentService class:
int icon = R.drawable.notif;
long when = System.currentTimeMillis();
NotificationManager notificationManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
Intent notificationIntent = new Intent(context, DemoActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, (int) when,
notificationIntent,0);
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.notify);
contentView.setImageViewResource(R.id.ImageViewpnotify, icon);
contentView.setTextViewText(R.id.TextViewpnotify, message);
notification.contentView = contentView;
notification.contentIntent= intent;
notification.icon = icon;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.ledARGB = 0xffff0000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledOnMS = 100;
notification.ledOffMS = 100;
notificationManager.notify((int)when, notification);
i know it is late to answer your question, but i had the same problem and i managed to solve it using the following link:
https://gist.github.com/mgartner/7092333
it is a bug in some devices where the service goes into idle state and stops receiving any notifications, the solution is by forcing it to restart every 5 min.
also you can use the following link to help u in customizing your gcmreceiver:
http://code.google.com/p/gcm/source/browse/gcm-client/src/com/google/android/gcm/GCMBaseIntentService.java?r=8094cc6410b7dc2db452eb19cc9274fda1b2d6a2
I know this question has been asked many times, but I'm still having issues getting it working.
In my application, I create a notification to prompt the user of some event and I'd like to play a sound that has been packaged as a raw resource within the application.
I create my notification instance:
Notification notification = new Notification(
R.drawable.some_image,
"some meaningful name",
System.currentTimeMillis() );
notification.setLatestEventInfo(...)
Then set some attributes on that notification:
notification.sound = Uri.parse( "android.resource://com.my.package/" + R.raw.some_mp3_file );
notification.flags = Notification.FLAG_INSISTENT;
Finally I invoke the NotificationManager to display the notification:
notificationManager.notify( 1, notification );
The notification does shows up, but the sound doesn't play.
Am I doing something wrong? Is there a <uses-permission> I'm missing? I can't see anything that I've done differently from anyone else that seems to have gotten it working.
For what it's worth, I'm testing directly on a Nexus 7.
try to look at below code. may help you out to solve your problems.
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.update;
CharSequence tickerText = "assignments";
long when = System.currentTimeMillis();
Notification assignmentNotification = new Notification(icon, tickerText, when);
**For sound** assignmentNotification.defaults |= Notification.DEFAULT_SOUND;
long[] vibrate = {0,100,200,300};
**For vibrate** assignmentNotification.vibrate = vibrate;
Context context = getApplicationContext();
CharSequence contentTitle = "check assignments";
CharSequence contentText = "chek ur app for assignments ";
Intent notificationIntent = new Intent(context, ViewAssignmentnotificationActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,0);
assignmentNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
** final int id = 2;
Please see this answer; It tells you that you have to explicitly specify notification.sound to play that tone;