How to open application while clicking notification? - android

I have implemented media notification and lock screen notification in my music player. While clicking lock screen notification, how to ask the password to the user and how to open the application.
As well as for media notification while clicking I have opened the application. But how to close the notification bar or how to hide the whole notification bar
and how to show the suggestion of my application in "open with " while clicking the music file in any file directory.

What do you mean by ask for password? just open an activity that asks for password.
As for open an activity use Nirali solution in: Open application after clicking on Notification
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, HomeActivity.class);
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;
notificationManager.notify(0, notification);

For "open with" you should define intent-filter in your manifest. Look at this register as music player and https://developer.android.com/guide/components/intents-filters.html

Related

android local notifications once a week

I am trying to implement local notifications on an android application that I am building. What I want to do is to have a local notification once a week, and when the user presses the notification then a new activity will appear with the actual notification text.
I tried the tutorial below but with no luck:
http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/
the notifications is not repeating every week and when i press it the first time that it appears, then it does not open the new activity.
Anyone who can give me a better tutorial or a good solution?
I don't know exactly how to set up your weekly notifications but i know how to start Activity when user click on Notification and clear the notification list after it:
There is declaration of Notification manager, and notification builder:
NotificationManager notificationManager = (NotificationManager)
Context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
Uri alarmSound = RingtoneManager.getDefaultUri(R.raw.ok);
notificationBuilder.setSound(alarmSound);
//There is declaration which Activity will be opened if user click on notification:
Intent notificationIntent = new Intent(context, Notifications.class);
//clear notification after clicking on it:
notificationBuilder.setAutoCancel(true);
PendingIntent newIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notificationBuilder.setContentIntent(newIntent);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher).setContentTitle("APP TITLE").setContentText("New notification");
notificationManager.notify(001, notificationBuilder.build());
I hope this will help you!

Android how to compose notification on notification bar

I am new to android. I want to now how to create a notification in the notification bar and compose it like 'usb connected' or 'usb debugging connected' notify. Can anyone help me with some codes?
I have used NotificationCompat.Builder for the purpose
First we have to style the notification
NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher) // icon to be displayed
.setContentTitle("Notification") // title for the notification
.setContentText(msg); // msg is some text you want to display
For Led light blinkering when notification is received
mBuilder.setLights(Color.BLUE,1000,1200);
To give notification sound you can make use of default notification sound set by the user
Uri sound=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(sound);
To cancel the notification when the user has selected it
mBuilder.setAutoCancel(true);
To start an activity in app when the user clicks the notification
Intent resultIntent = new Intent(this,ExampleActivity.class);
PendingIntent resultPendingIntent =PendingIntent.getActivity(this,0, resultIntent,0);
mBuilder.setContentIntent(resultPendingIntent);
Finally to show the notification in the bar make use of notification manager
NotificationManager mNotificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

How do I keep my Android notification displayed until my app is closed?

I've been developing for Android for awhile but this is my first shot at notifications. I've got my notification setup as described in the Android SDK tutorial, but I can't figure out how to keep the notification displayed until my app is closed. I want to disable that little minus sign at the end of the notification. I don't want my notification to disappear when a user clicks it. I would think there would be a notification flag... but I can't seem to figure this out. I'm developing on Android SDK 2.2. I know this is a simple question, and I apologize if this answer is already on here... I wasn't able to find exactly what I was looking for.
// Create notification manager
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Notification notification = new Notification(R.drawable.ic_launcher, "Ready", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, HomeActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// Make a notification
notification.setLatestEventInfo(getApplicationContext(), "Ready", "Select to manage your settings", contentIntent);
mNotificationManager.notify(0, notification);
You want FLAG_ONGOING_EVENT. Also try removing FLAG_NO_CLEAR and FLAG_AUTO_CANCEL if they are part of the defaults.

Android how to show notification on screen

I've been working on push notifications and I am able to implement it and display it on status bar, the problem I am facing is that I want to display it even if the phone is lock, Under the lock screen where it says ("drag to unlock"), I have seen notifications like that but cant find any example to that.
Example:
Just like when you received a missed call , it will show it under the lock button on your screen.
Code:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon_launcher;
CharSequence tickerText = "MyApplication";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE|Notification.DEFAULT_LIGHTS;;
CharSequence contentTitle = this.title;
CharSequence contentText = this.message;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTICE_ID, notification);
Create Notification using NotificationCompat.Builder
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher) // notification icon
.setContentTitle("Notification!") // title for notification
.setContentText("Hello word") // message for notification
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
Push Notification on locked Screen
http://www.hongkiat.com/blog/android-lock-screen-notifications/
Create Notification using NotificationCompat.Builder but make sure to put visibility to public like
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder
.setContentTitle("Title")
.setContentText("content")
.setSmallIcon(R.mipmap.ic_launcher)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);//to show content in lock screen
Have you tried creating the alertdialog with a flag? The flag_show_when_locked should do the trick.
Please refer to this thread, you should find a more detailed answer here.
Android Lock Screen Widget
I fixed this by adding this line to notification builder
builder.setOngoing(true);
It will also make notification not cancelable by user, but it solves the problem.
Credits to: Marian Klühspies (link)
The notifications you have seen may actually be widgets placed on a custom widget host lockscreen.
If you look at the android platform source code for InstallWidgetReceiver as late as 4.4.3 here:
https://android.googlesource.com/platform/packages/apps/Launcher3/+/master/src/com/android/launcher3/InstallWidgetReceiver.java
You will see this note by the author:
/**
* We will likely flesh this out later, to handle allow external apps to place widgets, but for now,
* we just want to expose the action around for checking elsewhere.
*/
And you can see that InstallWidgetReceiver.java is in fact not fleshed out by google in the same way as InstallShortCutReceiver.java is. So it seems at least up to 4.4.3 you cant add widgets to the native lock screen in the same way that you can for example add a shortcut to the homescreen using InstallShortCutReceiver.
Unless you build your own lockscreen app as a widget host and the user installs in lieu of the native you may be out of luck using a widget.
Another approach however is to just us an activity that sets getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
This will display your activity whether the screen is locked or not. Dismissing this activity when the screen is locked will display the locked screen.

Android : don't show Notification if app is shown

1/I don't want to notify user if this one is already running my app in foreground,
is it possible before create nofification to check if my app is not in front ?
2/if app is in background, is it possible to bring last state in front ?
I know that Os can destroy some Activity, but is it possible to restore last state, don't start a new Intent?
because if i start a new Intent and i push back, old Intent appear it's not very beautiful to have 2 identical intent launch.
Thanks
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Intent notificationIntent = new Intent(this, z_finish.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
is it possible before create nofification to check if my app is not in front ?
Your activities will have to tell the service when they are being paused and resumed; the service can then decide whether to display the Notification or not.
if app is in background, is it
possible to bring last state in front
?
I am not quite certain what "last state" is. Try FLAG_ACTIVITY_CLEAR_TOP in your Intent.
if your app is in singletask mode then the last state will be saved even if you relaunch it

Categories

Resources