I have searched a lot over the internet and could'n find an answer.
I am working on a launcher project and on a click of a button, I want to launch default notification bar . Is there any API.
you can implement the android default notification using the below code. you can customize.
// Invoking the default notification service
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this);
mBuilder.setContentTitle("ur titlw");
mBuilder.setContentText("ur content");
mBuilder.setTicker("ur sticker");
mBuilder.setSmallIcon(R.drawable.uricon);
// Increase notification number every time a new notification arrives
mBuilder.setNumber(++numMessagesOne);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ur activity where this notification shout appear);
resultIntent.putExtra("notificationId", "test");
// This ensures that navigating backward from the Activity leads out of
// the app to Home page
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent
stackBuilder.addParentStack(ur activity where it returns when click on the notification);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_ONE_SHOT // can only be used once
);
// start the activity when the user clicks the notification text
mBuilder.setContentIntent(resultPendingIntent);
myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// pass the Notification object to the system
myNotificationManager.notify(notificationIdOne, mBuilder.build());
I hope this will be useful.
Related
it's very weird and it happens just for my app. When I turn off my phone screen and turn it on again, it clears all of my notification despite it doesn't happens for other apps on my phone.
this is my code:
Intent resultIntent = resultIntent = new Intent(mContext, intentClass);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
resultIntent.putExtra("onvan", title);
resultIntent.putExtra("message", message);
resultIntent.putExtra("link", link);
resultIntent.putExtra("linktype", linktype);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder TSB = TaskStackBuilder.create(mContext);
TSB.addParentStack(intentClass);
TSB.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
TSB.getPendingIntent(
new Random().nextInt(),
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent);
I don't want to clear the notification when the screen turns off, I want it to clear when use manually clears it
what is wrong with my code?
You're using
setAutoCancel(true)
As per documentation
setAutoCancel(boolean autoCancel)
Make this notification automatically
dismissed when the user touches it.
I would try setting auto cancel to false.
I've read multiple questions and answers surrounding this issue, however I cannot get any of them to work for me.
I have a notification that on click I would like to bring the application to the front and resume rather than close and restart.
This is my notification code
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("example")
.setContentText("example");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, MainActivity.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.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);
int mId = 0;
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
And in my manifest file I have
android:launchMode="singleTop"
Can anyone see what is going wrong? I get no errors, although the notification refuses to resume the app and instead restarts it.
Fixed by using this:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Sound Asleep")
.setContentText("Click to play and stop sounds");
// Creates an explicit intent for an Activity in your app
final Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// 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.
// Adds the back stack for the Intent (but not the Intent itself)
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 0;
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
}
In Manifest
android:launchMode="singleTop"
I had some similar issue recently change your intent flags with this flags:
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Edit: and remove that launchmode from your manifest.
hope it help
Use below code, may it will help
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Sound Asleep")
.setContentText("Click to play and stop sounds");
// Creates an explicit intent for an Activity in your app
final Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// 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.
// Adds the back stack for the Intent (but not the Intent itself)
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_ACTIVITY_REORDER_TO_FRONT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 0;
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
I have a simple app that shows android notifications depending to one of three buttons you press.
Dog, Cat, or Mouse
If you press dog then a notification that says "Dog" shows.
If you press cat then a notification that says "Cat" shows.
If you press mouse then a notification that says "Mouse" shows.
When you click the notification it just takes me to my main activity (with the three buttons again). I would like it to take me to a separate activity, that will read what notification was clicked, and set it the text from the notification as a textView.
Any ideas on how to do 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());
Source:
http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Actions
Right now I have an alarm on my main activity which launches a class which launches a notification on the status bar. When I click on the notification, it opens up my main activity, now I want it to open a n specific view within that activity.
This is my notification code.
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = android.R.drawable.stat_notify_chat; // icon from resources
CharSequence tickerText = "TickerText"; // ticker-text
long when = System.currentTimeMillis(); // notification time
CharSequence contentTitle = "My notification"; // message title
CharSequence contentText = "Hello World!"; // message text
//This is the intent to open my main activity when the notification is clicked
final Intent notificationIntent = new Intent(context, mainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(notifID, notification);
This is the button in the layout used by my main activity which opens the view I want the notification to open (graph view).
<Button
android:id="#+id/graphsButton"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/reviewButton"
android:layout_toRightOf="#id/medicineButton"
android:onClick="openGraphs"
android:text="Graphs" />
When you click on the button. It executes the following method in the main class.
public void openGraphs(View v) {
canExit = false;
setContentView(R.layout.graphs);
}
So basically, I got the notification to open the app and launch the main activity, but I want it to launch the graph view directly.
Can anybody help me?
You could add a flag to the pending intent set in the notification using the intent's extras. Evaluate the intent when the activity is started. If you find the flag in the starting intent execute the the code in openGraphs(). Make sure to get the most recent intent (not the one which may have started the activity earlier, here is some advice on that: https://stackoverflow.com/a/6838082/1127492).
Is there anything stopping you from showing the graph directly in the Activity?
In the above Activity instead of having a button, to show the Graph when clicked, directly set the view to R.layout.graphs in onCreate() method.
In case you have the prescribed activity for some other purpose then create a separate activity just to show the graph and point to it from the notificationIntent.
I want to launch a notification. When I click on it, it opens a NEW window of the app.
Here's my code:
public class Noficitation extends Activity {
NotificationManager nm;
static final int uniqueID = 1394885;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent= new Intent (Intent.ACTION_MAIN);
intent.setClass(getApplicationContext(), SchoolBlichActivity.class);
PendingIntent pi=PendingIntent.getActivity(this, 0, intent, 0);
String body = " body";
String title = "title!";
Notification n =new Notification(R.drawable.table, body, System.currentTimeMillis());
n.setLatestEventInfo(this, title, body, pi);
n.defaults = Notification.DEFAULT_ALL;
n.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(uniqueID,n);
finish();
}
by the way, if i add nm.cancel(uniqueID) before the finish(), it creates the notification and immediately deletes it...
Thanks for the help :D
You might want to just add a notification in the notification bar, and when the user clicks it, it will launch the actual Activity. This way the user won't be interrupted in whatever he's doing.
Create the status bar notification like this:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification_icon, "Hello", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, myclass.class);
notification.setLatestEventInfo(getApplicationContext(), "My notification", "Hello world!", notificationIntent, PendingIntent.getActivity(this, 0, notificationIntent, 0));
mNotificationManager.notify(1, notification);
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Are you just trying to open a notification window in a current activity? Because if you are I dont think you need to launch it with an intent. You normally only use intents to launch new services or activities in your app unless youve built a custom view and activity/service which is to take place within the notification box. I see you have it set up in its own class which is fine but I think the way your doing it by default would open an entire new view.
If you need to launch a notification during a process or something like a button click you dont need to have the intent there.....or at least I never did :) What exactly are you trying to achieve with the notification.