I want notify a notification on a widget. but it doesn't show it
I call this function but in emulator android 4.3 just play sound when call this function. and it doesn't show any notification
void DisplayZiyaratPage()
{
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("Title")
.setContentText("text")
.setSound(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.alarm_sound))
.setDefaults(Notification.DEFAULT_VIBRATE);
Intent intent = new Intent(this, ZiyaratActivity.class);
PendingIntent pendingIntent =PendingIntent.getActivity(this, 0, intent, 0);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
try this
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.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);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
the NotificationCompat.Builder class has been added to the Support Package so we can use this to support API level v4 and up:
http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html
If your app supports versions of Android as old as API level 4, you can instead use NotificationCompat.Builder, available in the Android Support library.
Related
If there're two my app's notifications at device's system tray, the user choose one notification, the app's opening but all of other notifications is lost the click behavior, it means user cannot click them anymore. How can I prevent this action?
My code:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle(getString(R.string.test))
.setContentText(messageBody);
Intent intent = new Intent(this, NotifActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle = new Bundle();
bundle.putString(NotificationViewActivity.MESSAGE_EXTRA, messageBody);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt(50) + 1, builder.build());
int mId = (int) System.currentTimeMillis();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.imagename)
.setContentTitle("Title")
.setContentText("message")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, activityname.class);
//activityname is the name of activity which you want to launch
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.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);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
I detected my root cause in below code
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Because the request code is the same for all notifications, so when we click to the one notification, the other lost their click listener. Fix like below codes
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_ONE_SHOT);
I need heads-up notification which will be not cancelable from notification drawer so I created Heads-up Ongoing notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.logo_notification)
.setContentTitle("Title")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setGroup(KEY)
.setGroupSummary(true)
.setOngoing(true)
.setColor(context.getResources().getColor(R.color.tab_indicator_color));
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, Activity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(WActivity.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);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent pIntent = new Intent(context, PService.class);
pIntent.setAction("ACTION");
PendingIntent piPause = PendingIntent.getService(context, NOTIFICATION_ID, pIntent, 0);
mBuilder.addAction(icon, title, piPause);
Intent sIntent = new Intent(context, SService.class);
sIntent.setAction("ACTION");
PendingIntent piDismiss = PendingIntent.getService(context, NOTIFICATION_ID, sIntent, 0);
mBuilder.addAction(icon2, title2, piDismiss);
The problem is that After notification shows on the top of screen it does not hide to notification drawer. But if notification is not ongoing, it hides. I need heads-up notification which will hide to notification drawer.
AFAIK, a notification needs the following properties to be set to be shown as a heads-up notification.
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
setOngoing() means that the Notification is ongoing and cannot be hidden by the user. I believe that is probably why your Notification stays on the top and doesn't hide. Remove setOngoing(true) and it should work.
sendNotification('Hello', 0, 1);
private void sendNotification(String message, int id, int notification_num) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, Main.class);
intent.setData(new Uri.Builder().scheme("data").build());
intent.putExtra("id", id);
intent.putExtra("noti_number", notification_num);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(Main.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = PendingIntent.getActivity(this, id, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("App Title")
.setContentText(message)
.setSmallIcon(R.drawable.notif)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
notificationBuilder.setNumber(notification_num);
nm.notify(id, notificationBuilder.build());
}
When application is opened intent.putExtra works fine, but when I close app (kill proccess), notification is coming but not with it extra than with new intent extra.
Problem is also with multiple notification, when app is closed, there are no grouping like I want it to be.
notificationBuilder.setNumber is always 0 in background.
Need help how to run app always in background or how to continue previous intent.
I have done it using GcmIntentService & GcmBroadcastReceiver, for more info click on this link.
Below code works perfect in tablet but not in android phone. I want to show notification.
My code is:
if(trigId.equals("0")){
intent=new Intent(this,message_detail.class);
}else{
intent=new Intent(this,appinement_list.class);
}
PendingIntent pIntent=PendingIntent.getActivity(this, 0, intent, 0);
// nt=new Notification(R.drawable.ic_launcher, "Welco0me", System.currentTimeMillis());
Notification noti = new Notification.Builder(this)
.setContentTitle("Notification From EHIQ").setSound(alarmSound).setContentIntent(pIntent)
.setContentText(" "+Helper_MSGNotification.Subject.get(i)).setSmallIcon(aspl.scorehealth.R.drawable.ic_launcher).build();
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
noti.flags|=Notification.FLAG_AUTO_CANCEL;
manager.notify(i,noti);
I am confuse what is actually problem,please help me.
The NotificationCompat.Builder is the most easy way to create Notifications on all Android versions. You can even use features that are available with Android 4.1. If your app runs on devices with Android >=4.1 the new features will be used, if run on Android <4.1 the notification will be an simple old notification.
Simple replace Notification.Builder with NotificationCompat.Builder, and import android.support.v4.app.NotificationCompat.
To create a simple Notification just do:(see Android API Guide on Notifications)
NotificationCompat.Builder myNotification = new NotificationCompat.Builder(context)
.setContentTitle("Exercise of Notification!")
.setContentText("http://android-er.blogspot.com/")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
More Information about NotificationCompat.Builder
try this one,
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(getApplicationContext(), MYDEMOACTIVITY.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MYDEMOACTIVITY.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);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
In Eclipse Right click on project then choose Android Tools Add support Library Then Install
Changes will be following:
import android.support.v4.app.NotificationCompat;
NotificationCompat.Builder noti = new NotificationCompat.Builder(this);
I'm trying to make a notification when users pause my app. So to make it easier, users can go quickly to the application using notificaction. This is the code i'm using. It works for all versions before android 4 and i don't know which is the problem
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Titulo")
.setContentText("Titulo");
mBuilder.setOngoing(true);
// Creates an explicit intent for an Activity this
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
// put the flags
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.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);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
So when i press the notification in android 4.0 and higher, the activity is created again instead of resume. any help please, i can't make it work.
EDIT ( forget about manifest singletop )
android:launchMode="singleTop" same result, not working...
My activity contains a map. I'm using the new version of google maps. v2.
I just tried PendingIntent.FLAG_CANCEL_CURRENT and seems to work for me
public void showNotification(String header,String message){
// define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(this, MainActivity.class);
//PendingIntent.FLAG_CANCEL_CURRENT will bring the app back up again
PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this,PendingIntent.FLAG_CANCEL_CURRENT, intent, 0);
Notification mNotification = new Notification.Builder(this)
.setContentTitle(header)
.setContentText(message)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setSound(soundUri)
.addAction(R.drawable.ic_launcher, "View", pIntent)
.addAction(0, "Remind", pIntent)
.setOngoing(true)//optional
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotification);
}
Use android:launchMode="singleTop" in the manifest declaration of MainActivity
The only solution that actually worked for me after doing a lot of search is to do the following :
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).set...(...).set...(..);
Intent resultIntent = new Intent(this, MainClass.class);
resultIntent.setAction("android.intent.action.MAIN");
resultIntent.addCategory("android.intent.category.LAUNCHER");
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
this opens your current activiy without creating another one !
SOLUTION provided by #Patrick taken from the question and added as answer:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle(getString(R.string.txt))
.setContentText(getString(R.string.txt));
mBuilder.setOngoing(true);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, Activity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.from(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(Activity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.getNotification());