How can I open activity when notification is touched? - java

I am building a notification for my Music Player, so it displays what music is playing.
But I want to add a function where, when I touch it. It opens my layout player.xml. Which is deployed through the MainActivity.class.
I have researched on Notifications on developer.android.com and found a way to deploy and activity upon clicking the notification. But it did't work.
Here is my current code -
Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder.setContentTitle(songsList.get(songIndex).get("songTitle"));
builder.setContentText("NexPlay™");
builder.setTicker(songsList.get(songIndex).get("songTitle"));
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setSmallIcon(R.drawable.play_default);
builder.setAutoCancel(true);
builder.setPriority(0);
builder.setOngoing(true);
Notification notification = builder.build();
NotificationManager notificationManger =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManger.notify(01, notification);
Thanks to everyone for answering!
I added
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationToPlayer.class), 0);
builder.setContentIntent(pendingIntent);
to my notifications and it worked great !

Try this
private void showNotification() {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.service_started);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.android, text,
System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ServiceLauncher.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.service_label),
text, contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
// Send the notification.
// We use a layout id because it is a unique number. We use it later to cancel.
nm.notify(R.string.service_started, notification);
}
Hope this will solve your problem

You need to add intent on ur code:-
int icon = R.drawable.app_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message.split(":::")[1], when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = null;
notificationIntent = new Intent(context, MessageDetail.class);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, message.split(":::")[1], intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);

You'll also want to add an intent. Something like:
Intent actIntent = new Intent(this, do MyActivity.class);
Then in your builder:
.setContentIntent(PendingIntent.getActivity(MyActivity, 131314, actIntent,
PendingIntent.FLAG_UPDATE_CURRENT))

Related

Click on notification does not start activity

Click on notification does not start a specific activity
My code
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
int notificationId = (int) System.currentTimeMillis();
Intent picukUpIntent = new Intent(context, MainScreenActivity.class);
picukUpIntent.putExtra(MainScreenActivity.ORDER_ID, orderId);
picukUpIntent.putExtra(NOTI_TYPE, 3);
pendingIntent =
PendingIntent.getActivity(
context,
notificationId,
picukUpIntent,
PendingIntent.FLAG_ONE_SHOT
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_mini_logo)
.setContentTitle("Title")
.setContentText(message)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
mNotifyMgr.notify(notificationId, builder.build());
As you can see, I try to open MainScreenActivity activity when the user clicks on the notification and it does not start MainScreenActivity when clicked. If I replace MainScreenActivity with other acitivies then it works just fine.
Note that I have tried many solutions.
add android:exported="true" in activity tag in manifest
change flag to PendingIntent.FLAG_CANCEL_CURRENT
If I try to start other activities then it works. The problem ony happens when I try to start specifically MainScreenActivity
Replace this code
pendingIntent = PendingIntent.getActivity(
context,
notificationId,
picukUpIntent,
PendingIntent.FLAG_ONE_SHOT);
to
pendingIntent = PendingIntent.getActivity(
context,
notificationId,
picukUpIntent,
PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);

Android How to show alert on another app

Hi I want to show alert after click on my notification. Here is code:
#SuppressWarnings("deprecation")
private void Notify(String notificationTitle, String notificationMessage) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon_stat, "Powiadomionko", System.currentTimeMillis());
notification.ledARGB = Color.CYAN;//0xFFff0000;
notification.ledOnMS = 800;
notification.ledOffMS = 2400;
notification.vibrate = new long[]{100, 120, 100, 120};
Intent notificationIntent = new Intent(this, TerminarzActivity.class);
notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_VIBRATE;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(Serwis_updateTERM.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify(1, notification);
}
And I want to show Alert on anything not on my Activity after click. Anyone know how to do this ?
I know i must add
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
but nothing else..
Check out the creation of this notification (taken from Geofence sample). This code creates a notification and if you touch it it launches your MainActivity.
// Create an explicit content Intent that starts the main Activity
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
// Construct a task stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the main Activity to the task stack as the parent
stackBuilder.addParentStack(MainActivity.class);
// Push the content Intent onto the stack
stackBuilder.addNextIntent(notificationIntent);
// Get a PendingIntent containing the entire back stack
PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Get a notification builder that's compatible with platform versions
// >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Set the notification contents
builder.setSmallIcon(R.drawable.ic_folder)
.setContentTitle(getString(R.string.geofence_transition_notification_title, transitionType, ids))
.setContentText(getString(R.string.geofence_transition_notification_text))
.setContentIntent(notificationPendingIntent);
// Get an instance of the Notification manager
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(0, builder.build());
This code might help you, but still it depends what do you mean by "alert".

Clearing Notification from invoked Activity

I use this code to show a notification:
public static void ShowCustomNotification(Context context,String notificationTitle, String notificationMessage)
{
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(context, ResultActivity.class);
intent.putExtra("title", notificationTitle);
intent.putExtra("body", notificationMessage);
intent.setFlags(PendingIntent.FLAG_UPDATE_CURRENT
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
Notification mNotification = new NotificationCompat.Builder(context)
.setContentTitle(notificationTitle)
.setSmallIcon(R.drawable.office_girl)
.setContentIntent(pIntent)
.setSound(soundUri)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotification);
}
When the user clicks the Notification the activity ResultActivity.class is invoked and everything is fine.
My question is how can i clear the notification on the status bar when a user click on a "Close" button from inside ResultActivity? in case the user click "Back" i dont want to clear the notification.
Thanks.
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(YOUR_NOTIFICATION_ID);

Multiple push notifications on android with wrong icon size

Let me preface this post with the fact that I am new to the android programming.
When I issue a notification, the phone shows two notifications for the same thing. The only difference is, the icons look different and they do different things. The first will do nothing when you select it. The other opens the app to the correct page.
I used to only get one notification until I edited this method. I'm not sure what I did but perhaps someone could tell me why this sends 2 and how to fix it?
The other question I have is that on both notifications on the phone, the icon is zoomed in (or too big). How do you make the push notification icon the correct size?
Here is my method
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, FriendGroupActivity.class);
// set intent so it does not start a new activity
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;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
Try this :
private void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Notification notification;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, myactivity.class), 0);
// To support 2.3 os, we use "Notification" class and 3.0+ os will use
// "NotificationCompat.Builder" class.
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
notification = new Notification(icon, message, 0);
notification.setLatestEventInfo(context, appname, message,
contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(appname).setWhen(0)
.setAutoCancel(true).setContentTitle(appname)
.setContentText(message).build();
notificationManager.notify(0 , notification);
}
}
Hope this helps.This works for me.

Use notification ID to change logo & text

I'd like to know how applications like Facebook change their notification title & logo depending on the content.
For example, in Facebook, if you get tagged you get another title & another logo.
I assume it should be possible with notify to create a unique notification.
Though I can't find any clear examples for this.
My GenerateNotification:
private static void generateNotification(Context context, String message, String url) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, ShowChange.class);
notificationIntent.putExtra ("url",url);
// set intent so it does not start a new activity
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;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
It should be something like this:(this is basically your code with some modifications)
public static void generateNotification(Context context, String message, String url,int icon_from_drawable) {
int icon = icon_from_drawable;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, ShowChange.class);
notificationIntent.putExtra ("url",url);
// set intent so it does not start a new activity
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;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
If you targeting your app for API LEVEL>=11, then use Notification.Builder
refer here : http://developer.android.com/reference/android/app/Notification.Builder.html
One more point the above code will keep updating the same notification object because the notify method of notification manager is taking always the same id i.e 0, so the same object of notification will be updated.

Categories

Resources