I have code for creating notification:
NotificationManager notifyMngr=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification=new Notification(mId, "New alert!", System.currentTimeMillis());
Intent alert = new Intent(this, AlertInfoActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, alert, 0);
notification.setLatestEventInfo(this, data.get("name"), data.get("post_date"), contentIntent);
notifyMngr.notify((int) System.currentTimeMillis(), notification);
I need to create a few notifications, and each notification must execute a new object of AlertInfoActivity by click. But this code executes 1 object of Activity always. How can I do my task?
You need to create a notification for each activity you want to launch. Like this:
NotificationManager notifyMngr=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//First Notification
Notification notification=new Notification(mId, "New alert!", System.currentTimeMillis());
Intent alert = new Intent(this, AlertInfoActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, alert, 0);
notification.setLatestEventInfo(this, data.get("name"), data.get("post_date"), contentIntent);
notifyMngr.notify((int) System.currentTimeMillis(), notification);
//Second Notification
Notification notification2 = new Notification(mId, "Second Alert!", System.currentTimeMillis());
Intent alert2 = new Intent(this, DifferntAcitivty.class);
PendingIntent contentIntent2 = PendingIntent.getActivity(this, 0, notification2, 0);
notification2.setLatestEventInfo(this, data.get("name"), data.get("post_date"), contentIntent2);
notifyMngr.notify((int) System.currentTimeMillis(), notification2);
//Third Notification
Notification notification3=new Notification(mId, "Third Alert!", System.currentTimeMillis());
Intent alert3 = new Intent(this, ThirdActivity.class);
PendingIntent contentIntent3 = PendingIntent.getActivity(this, 0, alert3, 0);
notification3.setLatestEventInfo(this, data.get("name"), data.get("post_date"), contentIntent3);
notifyMngr.notify((int) System.currentTimeMillis(), notification3);
//And so on and so forth.
Related
notification in nexus 5 android 6 don't cancel and go away from it. but when i run it on diffrenet device with android 9 or more, notification goes away. please help me.
public void showNotification(int playPauseBtn)
{
Intent intent = new Intent(this, PlayerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent PrevIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_PREVIOUS);
PendingIntent prevPending = PendingIntent.getBroadcast(this, NOTIFICATION_ID, PrevIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent pauseIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_PLAY);
PendingIntent pausePending = PendingIntent.getBroadcast(this, NOTIFICATION_ID, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent nextIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_NEXT);
PendingIntent nextPending = PendingIntent.getBroadcast(this, NOTIFICATION_ID, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
byte[] picture;
picture = getTrackArt(musicFiles.get(position).getPath());
Bitmap thumb;
if (picture != null)
{
thumb = BitmapFactory.decodeByteArray(picture, 0, picture.length);
} else
{
thumb = BitmapFactory.decodeResource(getResources(), R.drawable.musicnowplaying);
}
Notification notification = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID_2)
.setSmallIcon(playPauseBtn)
.setLargeIcon(thumb)
.setChannelId(CHANNEL_ID_2)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setOngoing(true)
.setContentTitle(musicFiles.get(position).getSongTitle())
.setContentText(musicFiles.get(position).getArtist())
.addAction(R.drawable.ic_skip_previous, "Previous", prevPending)
.addAction(playPauseBtn, "Pause", pausePending)
.addAction(R.drawable.ic_skip_next, "Next", nextPending)
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setMediaSession(mediaSessionCompat.getSessionToken()))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOnlyAlertOnce(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.build();
startForeground(NOTIFICATION_ID, notification);
}
I am building a notification which has ACCEPT and REJECT buttons but at the same time i also wants to open the activity when user touches over the notification. But in my case only both buttons working and when touched over the other parts than buttons it just disappears.
My efforts :
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
String NOTIFICATION_CHANNEL_ID = "BizPhone Channel";
int id = (int) System.currentTimeMillis();
Intent intent = new Intent(context, incoming_service.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(Intent.EXTRA_PHONE_NUMBER, phoneNumber);
intent.putExtra(Intent.EXTRA_MIME_TYPES, callType);
intent.putExtra("NOTE", Integer.toString(id));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "BizPhone Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("BizPhone related notifications");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
// assuming your main activity
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setCategory(Notification.CATEGORY_CALL)
.setOngoing(true)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_call_black_24dp)
.setPriority(Notification.FLAG_INSISTENT)
.setContentTitle(phoneNumber)
.setContentText("Incoming Call")
.setSound(alarmSound)
.setContentIntent(pendingIntent)
.setContentInfo("Call");
Intent accept = new Intent(context, incoming_service.class);
accept.setAction(incoming_service.ANSWER);
accept.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
accept.putExtra(Intent.EXTRA_PHONE_NUMBER, phoneNumber);
accept.putExtra(Intent.EXTRA_MIME_TYPES, callType);
accept.putExtra("NOTE", Integer.toString(id));
PendingIntent pendingIntentYes = PendingIntent.getService(context, 10, accept, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.addAction(R.drawable.ic_permission_granted_24dp, "ACCEPT", pendingIntentYes);
Intent decline = new Intent(context, incoming_service.class);
decline.setAction(incoming_service.REJECT);
decline.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
decline.putExtra(Intent.EXTRA_PHONE_NUMBER, phoneNumber);
decline.putExtra(Intent.EXTRA_MIME_TYPES, callType);
decline.putExtra("NOTE", Integer.toString(id));
PendingIntent pendingIntentNo = PendingIntent.getService(context, 11, decline, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.addAction(R.drawable.ic_call_end_black_24dp, "REJECT", pendingIntentNo);
//notificationBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(id, notificationBuilder.build());
I have tried by looking at other threads too but it did not help. Any help would be appreciated and thanks in advance.
Change
setAutoCancel(true)
to
setAutoCancel(false)
Just try to add :
PendingIntent contentIntent = builder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
I am answering my own question for future incoming users.
Why notification was disappearing on touch ( outside of 2 buttons but still on notification )
Because the base pendingintent was not a activity and it was a service. You can notice the code above. It should have been like :
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClass(context, CallActivity.class);
intent.putExtra(Intent.EXTRA_PHONE_NUMBER, phoneNumber);
intent.putExtra(Intent.EXTRA_MIME_TYPES, callType);
And the 2 buttons code and their pendingintent which is service was okay.
I'm trying to schedule a method to be called using AlarmManager, but it doesn't seem to be working. I've looked at other examples and theirs isn't working for me. So I'm thinking it's something from my code. Here's the AlarmManager code:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(SplashScreenActivity.this, KinectReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(SplashScreenActivity.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 25);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
And my broadcast receiver:
public class KinectReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Done", Toast.LENGTH_LONG).show();
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_notify)
.setContentTitle("Kinect")
.setColor(ContextCompat.getColor(context, R.color.colorAccent))
.setWhen(System.currentTimeMillis())
.setContentText("Your Kinects and Likes have been refilled. Now get to swiping")
.setAutoCancel(true)
.setSound(uri)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Your Kinects and Likes have been refilled. Now get to swiping"))
.setVibrate(new long[] { 100, 500, 100, 500, 100 });
Intent targetIntent = new Intent(context, MainActivity.class);
targetIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
targetIntent.putExtra("action", "main");
targetIntent.putExtra("id", "");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
}
Neither the Toast nor the Notification show.
The AlarmManager code is in the first thing that runs in my launcher activity, in case that helps. Thanks
Please register ur receiver in manifeast file like this
In application tag
<receiver
android:name=".KinectReceiver">
Second question is how to cancel alarm
Here is ur answer
Intent myIntent = new Intent(MainActivity.this, AlarmActivity.class);
pendingIntent = PendingIntent.getActivity(CellManageAddShowActivity.this,
id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent.cancel();
alarmManager.cancel(pendingIntent);
The main thing that you will need is:
1).Create pending intent with the same id and appropriate intent FLAG.
2).Cancel that pending intent.
3).Cancel the alarm using alarm manager.
I want to open a website when a push notification is clicked on Android.
This is my code, and it opens the main Activity when I click the notification
. How can I open a website / URL in the browser instead ?
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setContentTitle("Big4Com").setSmallIcon(R.drawable.notification_icon).setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
Try this:
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://your-website.com"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setContentTitle("Big4Com").setSmallIcon(R.drawable.notification_icon).setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
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);