//Define Notification Manager NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); //Define sound URI Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext()) .setSmallIcon(icon) .setContentTitle(title) .setContentText(message) .setSound(soundUri); //This sets the sound to play //Display notification notificationManager.notify(0, mBuilder.build());
NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("title") .setAutoCancel(true) .setContentText("content") .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0)); NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, mBuilder.build());
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);
@Override public void onCreate() { NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.notification_icon, "Notify Alarm strart", System.currentTimeMillis()); Intent myIntent = new Intent(this , MyActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); notification.setLatestEventInfo(this, "Notify label", "Notify text", contentIntent); mNM.notify(NOTIFICATION, notification); }
// prepare intent which is triggered if the // notification is selected Intent intent = new Intent(this, NotificationReceiver.class); PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); // build notification // the addAction re-use the same intent to keep the example short Notification n = new Notification.Builder(this) .setContentTitle("New mail from " + "test@gmail.com") .setContentText("Subject") .setSmallIcon(R.drawable.icon) .setContentIntent(pIntent) .setAutoCancel(true) .addAction(R.drawable.icon, "Call", pIntent) .addAction(R.drawable.icon, "More", pIntent) .addAction(R.drawable.icon, "And more", pIntent).build(); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(0, n);
Intent intent = new Intent(this, cls); intent.putExtra("extra", extra); PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), id, intent, PendingIntent.FLAG_ONE_SHOT); notifyDetails.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent); mNotificationManager.notify(id, notifyDetails);
Intent notificationIntent = new Intent(mContext, HandleNotificationClickService.class); PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, notificationIntent, 0); Notification notification = new Notification(icon, tickerText,System.currentTimeMillis()); notification.setLatestEventInfo(mContext,contentTitle , contentText, pendingIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT; NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(CALLER_ID_NOTIFICATION_ID, notification);
private void raiseNotification(String contentType, File output, Exception e) { NotificationCompat.Builder b= new NotificationCompat.Builder(this, CHANNEL_WHATEVER); b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL) .setWhen(System.currentTimeMillis()); if (e == null) { b.setContentTitle(getString(R.string.download_complete)) .setContentText(getString(R.string.fun)) .setSmallIcon(android.R.drawable.stat_sys_download_done); Intent outbound=new Intent(Intent.ACTION_VIEW); outbound.setDataAndType(Uri.fromFile(output), contentType); b.setContentIntent(PendingIntent.getActivity(this, 0, outbound, 0)); } else { b.setContentTitle(getString(R.string.exception)) .setContentText(e.getMessage()) .setSmallIcon(android.R.drawable.stat_notify_error); } mgr.notify(NOTIFY_ID, b.build()); }
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hello from service", System.currentTimeMillis()); Intent intent = new Intent(this, MainActivity.class); notification.setLatestEventInfo(this, "contentTitle", "contentText", PendingIntent.getActivity(this, 1, intent, 0)); manager.notify(111, notification);
/** * Updates the notification, and the play/pause button in it. * Used for changes on the remoteView * * @param drawableId if != -1, sets the drawable with that id on the play/pause button */ private void updateNotification(int drawableId) { if (DEBUG) Log.d(TAG, "updateNotification() called with: drawableId = [" + drawableId + "]"); if (notBuilder == null || notRemoteView == null) return; if (drawableId != -1) notRemoteView.setImageViewResource(R.id.notificationPlayPause, drawableId); notificationManager.notify(NOTIFICATION_ID, notBuilder.build()); }
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notId + selectedPosition, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis() - offset, pendingIntent); Notification notification = new Notification(R.drawable.icon, "TVGuide Υπενθύμιση", System.currentTimeMillis()); NotificationManager manger = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); notification.setLatestEventInfo(context, "Κανάλι: " + b.getString("channel"), "Εκπομπή: " + showname, pendingIntent); manger.notify(notId, notification);
@Subscribe public void onDeadEvent(DeadEvent braiiiiiiinz) { RandomEvent original=(RandomEvent)braiiiiiiinz.event; NotificationCompat.Builder b=new NotificationCompat.Builder(this); Intent ui=new Intent(this, EventDemoActivity.class); b.setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND) .setContentTitle(getString(R.string.notif_title)) .setContentText(Integer.toHexString(original.value)) .setSmallIcon(android.R.drawable.stat_notify_more) .setTicker(getString(R.string.notif_title)) .setContentIntent(PendingIntent.getActivity(this, 0, ui, 0)); NotificationManager mgr= (NotificationManager)getSystemService(NOTIFICATION_SERVICE); mgr.notify(NOTIFY_ID, b.build()); } }
public class ReminderService extends IntentService { private static final int NOTIF_ID = 1; public ReminderService(){ super("ReminderService"); } @Override protected void onHandleIntent(Intent intent) { NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); long when = System.currentTimeMillis(); // notification time Notification notification = new Notification(R.drawable.icon, "reminder", when); notification.defaults |= Notification.DEFAULT_SOUND; notification.flags |= notification.FLAG_AUTO_CANCEL; Intent notificationIntent = new Intent(this, YourActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0); notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent); nm.notify(NOTIF_ID, notification); } }
ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder binder) { ((KillBinder) binder).service.startService(new Intent( MainActivity.this, KillNotificationsService.class)); Notification notification = new Notification( R.drawable.ic_launcher, "Text", System.currentTimeMillis()); Intent notificationIntent = new Intent(MainActivity.this, Place.class); PendingIntent contentIntent = PendingIntent.getActivity( MainActivity.this, 0, notificationIntent, 0); notification.setLatestEventInfo(getApplicationContext(), "Text", "Text", contentIntent); NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mNM.notify(KillNotificationsService.NOTIFICATION_ID, notification); } public void onServiceDisconnected(ComponentName className) { } }; bindService(new Intent(MainActivity.this, KillNotificationsService.class), mConnection, Context.BIND_AUTO_CREATE);
private void raiseNotification(Intent inbound, File output, Exception e) { NotificationCompat.Builder b= new NotificationCompat.Builder(this, CHANNEL_WHATEVER); b.setAutoCancel(true).setWhen(System.currentTimeMillis()); if (e == null) { b.setContentTitle(getString(R.string.download_complete)) .setContentText(getString(R.string.fun)) .setSmallIcon(android.R.drawable.stat_sys_download_done); Intent outbound=new Intent(Intent.ACTION_VIEW); Uri outputUri= FileProvider.getUriForFile(this, AUTHORITY, output); outbound.setDataAndType(outputUri, inbound.getType()); outbound.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); PendingIntent pi=PendingIntent.getActivity(this, 0, outbound, PendingIntent.FLAG_UPDATE_CURRENT); b.setContentIntent(pi); } else { b.setContentTitle(getString(R.string.exception)) .setContentText(e.getMessage()) .setSmallIcon(android.R.drawable.stat_notify_error); } NotificationManager mgr= (NotificationManager)getSystemService(NOTIFICATION_SERVICE); mgr.notify(NOTIFY_ID, b.build()); }
System.currentTimeMillis()); notification.flags |= Notification.FLAG_AUTO_CANCEL; Intent intent = new Intent(this, HomeScreenActivity.class); intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP); notification.setLatestEventInfo(this, title, text, contentIntent); mNM.notify(R.string.app_name, notification);
private void createNotification(String text, String link){ NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setAutoCancel(true) .setSmallIcon(R.drawable.app_icon) .setContentTitle(text); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // pending implicit intent to view url Intent resultIntent = new Intent(Intent.ACTION_VIEW); resultIntent.setData(Uri.parse(link)); PendingIntent pending = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); notificationBuilder.setContentIntent(pending); // using the same tag and Id causes the new notification to replace an existing one mNotificationManager.notify(String.valueOf(System.currentTimeMillis()), PUSH, notificationBuilder.build()); }
protected void onMessage(Context context, Intent intent) { Log.d(TAG, "onMessage - context: " + context); // Extract the payload from the message Bundle extras = intent.getExtras(); if (extras != null) { boolean foreground = this.isInForeground(); extras.putBoolean("foreground", foreground); if (foreground){ PushHandlerActivity.sendToApp(extras); }else{ String message = extras.getString("message"); String title = extras.getString("title"); Notification notif = new Notification(android.R.drawable.btn_star_big_on, message, System.currentTimeMillis() ); notif.flags = Notification.FLAG_AUTO_CANCEL; notif.defaults |= Notification.DEFAULT_SOUND; notif.defaults |= Notification.DEFAULT_VIBRATE; String url = "notify"; Intent notificationIntent = new Intent(context, MyPhoneGapActivity.class); //here you pass the information notificationIntent.putExtra ("url",url); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); notif.setLatestEventInfo(context, title, message, contentIntent); String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns); mNotificationManager.notify(1, notif); } } }