android - notifications without alerting the user - android

I currently have an ongoing notification that displays some information about my app. While this information is useful, there's no need for the user to actually see that I've created the notification. Its basically there to tell them that "hey, my app is doing stuff in the background, click here to go back to it".
My notification works correctly, however whenever I call ".notify()" my notification shows the text from my notification in the notification bar at the top of the screen, like a text message does.
I want to to do it silently, so when I call notify it doesn't show my notifications text in the small notification bar before doing the "roll up" animation.
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this, AudioPlayer.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new Notification(R.drawable.ic_launcher,
title, System.currentTimeMillis());
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.note_layout);
~~~ remote views stuff ~~~
contentView.setTextViewText(R.id.notification_title, title);
contentView.setTextViewText(R.id.notification_text, author);
notification.contentView = contentView;
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(8675309, notification);

I'm not sure that the OS allow that.
What you want to hide is called Ticker.
You can try to set an empty Ticker Text:
Notification notification = new Notification(R.drawable.ic_launcher,
"", System.currentTimeMillis());

Related

what to code for notification to stick until music stop playing

I want notification which will stick on notification bar until music stop playing it.
currently i have written some code in which i can show notification but when i press clear notification button or swap it than it disappear from notification center.
I want notification like spotify which stays on bar until you stop playing music.
Here is my code for notification
int pendingRequestCode = 0;
// final Resources res = getResources();
notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
Intent i = new Intent(getApplicationContext(),Mainactivity.class);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_action_search)
.setAutoCancel(true)
.setTicker("test ckick")
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), NOTIFICATION_DEFAULT, i,0));
// Sets a custom content view for the notification, including an image button.
layout = new RemoteViews(getPackageName(), R.layout.notification);
layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
Intent clickIntent = new Intent();
clickIntent.setAction(ACTION_DIALOG);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
layout.setOnClickPendingIntent(R.id.notification_button,pendingIntent);
builder.setContent(layout);
// Notifications in Android 3.0 now have a standard mechanism for displaying large
// bitmaps such as contact avatars. Here, we load an example image and resize it to the
// appropriate size for large bitmaps in notifications.
layout.setImageViewResource(R.id.notification_button, R.drawable.pause);
notificationManager.notify(NOTIFICATION_DEFAULT, builder.getNotification());
waiting for reply
Use setOngoing(true) to indicate that the event is ongoing. You may also wish to remove setAutoCancel(true), as that clears the Notification when the user taps upon it.
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL

Click on status bar notification and show details?

I wrote an app that saves local notification at specific time in future and when that time is reached it shows notification in status bar even if the app is not running. My question is: it is possible to show some additional information to user when he clicks on notification in status bar?
Yes it is possible to display additional information when user taps on that particular notification. While writing code to display notification, you will find one setLatestEventInfo method. Use it. Its exactly what you need.
public void createNotification(View view) {
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
"A new notification", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "This is the title",
"This is the text", activity);
notification.number += 1;
notificationManager.notify(0, notification);
}

when to clear notification count of status notification in android

I have a method for creating notification. I want to clear notification number as soon as a user clicks on status notification.
public void createNotification(){
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification=null;
notification = new Notification(R.drawable.ic_launcher, "You have a new message", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.number = **count**++;
Intent intent = new Intent();
intent.setClass(TabInterfaceActivity.this, TabInterfaceActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = activity;
notification.setLatestEventInfo(this, " New Message", message, notification.contentIntent);
}
If by "I want to clear notification number" you mean you want to keep the notification itself still displayed, but you want just to update/remove counter, then you should set your notification PendingIntent to point to your code that would handle this scenario for you, and when it completes it shall redirect to your target TabInterfaceActivity (or you can make it more flexible and pass target in PendingIntent's Bundle).

Displaying a notification in android

I am using the following code to show a notification in my android application, but using this, with the notification an activity also shows up (i.e. a black blank screen) I dont want this screen, but just a simple notification and when the user clicks the notification then I want to launch the activity. What should be done to this code?
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification(R.drawable.mcube_logo, "Message Sent", Calendar.getInstance().getTimeInMillis());
Intent intent = new Intent(RecieveAlarm.this, otherActivity.class);
PendingIntent pendingIntent =PendingIntent.getActivity(RecieveAlarm.this, 1, null, PendingIntent.FLAG_CANCEL_CURRENT);
notify.setLatestEventInfo(RecieveAlarm.this, "Title", "Details", pendingIntent);
notificationManager.notify(0, notify);
I suggest you to use:
Notification not = new Notification(idIcon, text, System.currentTimeMillis());
PendingIntent pInt = PendingIntent.getActivity(ctx, 0, new Intent(ctx, the_class_to_call), 0);
not.setLatestEventInfo(ctx, app_name, text, pInt);
and then not.notify....
use AlertDialog, then launch your desired activity on positive button click

Flagging issues in android

I'm trying to get my Notification to not cancel when the user presses "Clear All" So far I have the intent working properly on everything except this:
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.addFlags(Notification.FLAG_ONGOING_EVENT);
intent.addFlags(Notification.FLAG_NO_CLEAR);
PendingIntent contentIntent = PendingIntent.getActivity(
getBaseContext(), 0, intent, 0);
The question I have at this point is: Are my flags correct?
Yes, your flags look pretty much correct, although I don't know if you even need the FLAG_NO_CLEAR. I currently have an app which creates an ongoing (non-cancellable) notification - I only use the FLAG_ONGOING_EVENT and it works fine for me. I pretty much just copied it from a tutorial and then added the ongoing event flag.
Here's some sample code:
String text = "notification";
Notification notification = new Notification(R.drawable.icon, text,
System.currentTimeMillis());
//launch the activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent().setComponent(ComponentName.unflattenFromString("com.example/com.example.MyActivity")), 0);
// set the label and text...
notification.setLatestEventInfo(this, getText(R.string.notification_label),
text, contentIntent);
notification.flags = Notification.FLAG_ONGOING_EVENT;
// Send the notification.
// We use a string id because it is a unique number. We use it later to cancel.
NotificationManager mNM;
mNM.notify(R.string.notification_label, notification);

Categories

Resources