After looking at the official document of the Android studio, I created a program that alerts me according to the situation I wanted.(Actually, I just copied and pasted it.)
However, notifications only appear in the top bar now.
I'd like to have a notification window appear on the lock screen where my phone is turned off.
A notification sound is coming when the cell phone is turned off.
Do I need to design a new window in xml?
Or is there a problem?
I'm sorry if the question is weird.I look forward to your advice. Thank you.
Here is my code (Notification Method)
private void showNoti() {
builder=null;
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
manager.createNotificationChannel(
new NotificationChannel(CHANNEL_ID,CHANEL_NAME,NotificationManager.IMPORTANCE_DEFAULT)
);
builder = new NotificationCompat.Builder(this,CHANNEL_ID);
}else{
builder=new NotificationCompat.Builder(this);
}
Intent intent = new Intent(this, mapActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Intent fullScreenIntent = new Intent(this, mapActivity.class);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent snoozeIntent = new Intent(this,mapActivity.class);
snoozeIntent.putExtra(EXTRA_NOTIFICATION_ID,0);
PendingIntent snoozePendingIntent =
PendingIntent.getBroadcast(this,0,snoozeIntent,0);
builder.setContentTitle("화재알림");
builder.setContentText("화재가 발생했습니다");
builder.setSmallIcon(R.drawable.fire);
builder.setContentIntent(pendingIntent);
builder.addAction(R.drawable.fire, getString(R.string.app_name),
snoozePendingIntent);
builder.setFullScreenIntent(fullScreenPendingIntent, true);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setAutoCancel(true);
Notification notification = builder.build();
manager.notify(1,notification);
}
Related
I'm pretty new to Android development. I have been able to get a notification to pop up while the app is in the background. When I click on it, it successfully loads the application backup. However I want to load an Alert from the page but only when it is opened from a notification click.
Here is the code for generating the notification. Any help would be appreciated.
private void getNotificationForPasswordChange() {
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Hello";// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
if (mNotificationManager != null)
mNotificationManager.createNotificationChannel(mChannel);
}
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher);
Intent i=new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent mainIntent = PendingIntent.getActivity(this, 0,
i, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Pronto Tracker")
.setTicker("Pronto Tracker")
.setContentText("Cannot connect to server. Location is not being updated.")
.setSmallIcon(R.mipmap.ic_pronto_logo)
.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
.setOngoing(true).setContentIntent(mainIntent).
build();
mNotificationManager.notify(Constants.PASSWORD_CHANGE_NOTIFICATION_ID, notification);
}
You can pass the alert message with notification PendingIntent. Add the message or value you want to show as alert in PendingIntent .putExtra() and also specify the activity in PendingIntent where you want to show the alert in form of dialog or anything.
Intent intent = new Intent(Application.getAppContext(), MainActivity.class);
intent.putExtra("is_notification", true);
intent.putExtra("alert_message", "Hello World!");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent lowIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
After that add the PendingIntent to your notification.
Second thing you need to do is to get the data from the Intent when user taps on notification.
In your MainActivity add the following code to get data from Intent:-
if (getIntent() != null) {
String message = getIntent().getStringExtra("alert_message");
boolean isNotification = getIntent().getBooleanExtra("is_notification", false);
if(is_notification){
// show alert
}
}
You should use onCreate function on your MainActivity
Add this code to parce your intent:
Intent receivedIntent = getIntent();
I have implemented some notifications in my application. It seems that everytime I click on the notification it starts a new intent. The goal is to bring the application in the foreground because it is still in background (so not killed).
How can I achieve this?
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ChatActivity.class), 0);
long[] vibration = new long[]{100, 250};
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setAutoCancel(true)
.setTicker(msg)
.setLights(Color.WHITE, 300, 600)
.setSound(Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.new_msg))
.setVibrate(vibration)
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
To bring the application to the front, use the following intent in the Pending Intent.
Intent intent = new Intent(this, ChatActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
This'll resume the application if not already killed or open the ChatActivity if it has been killed.
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".
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))
I have successfully created a notification thanks to my other question, using NotificationCompat. I want to now just change what the notification does onClick. I'd really just like to have an alert dialog pop up (I've seen some app's do it) but everytime I click it, I just have my activity show up. Any ideas?
The notification code:
Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
YOUR_PI_REQ_CODE, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.some_img)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
.setTicker(res.getString(R.string.your_ticker))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.your_notif_title))
.setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();
nm.notify(YOUR_NOTIF_ID, n);
You can't have a normal dialog without an activity. There are several possible workarounds though including styling the activity like a dialog and making the activity itself invisible and launching a dialog from it immediately.
You can probably use Remote Views to show a dialog without going to your app process.
you can set
Intent intent = new Intent(context, ReserveStatusActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
intent = new Intent(String.valueOf(PushActivity.class));
intent.putExtra("message", MESSAGE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(PushActivity.class);
stackBuilder.addNextIntent(intent);
// PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.BigPictureStyle notiStyle = new
NotificationCompat.BigPictureStyle();
android.app.Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(en_title)
.setContentText(en_alert)
.setAutoCancel(true)
.setNumber(++numMessages)
.setStyle(notiStyle)
//.setContentIntent(resultPendingIntent)
.setContentIntent(pendingIntent)
.build();
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager.notify(1000, notification);