Example usage for android.app NotificationManager notify

List of usage examples for android.app NotificationManager notify

Introduction

In this page you can find the example usage for android.app NotificationManager notify.

Prototype

public void notify(int id, Notification notification) 

Source Link

Document

Post a notification to be shown in the status bar.

Usage

From source file:com.androdevlinux.percy.bitfly.Core.Services.MyFirebaseMessagingService.java

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 *//*from   w  w  w. j a  v a2  s .c  o m*/
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_menu_camera).setContentTitle("FCM Message").setContentText(messageBody)
            .setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

From source file:azzaoui.sociadee.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received./*w  w w  . j  ava  2s .  c  o  m*/
 */
private void sendNotification(String message) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.pp_swag).setContentTitle("GCM Message").setContentText(message)
            .setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

From source file:com.commonsware.android.sawmonitor.PackageReceiver.java

private void seeSAW(Context ctxt, String pkg, boolean isReplace) {
    if (hasSAW(ctxt, pkg)) {
        Uri pkgUri = Uri.parse("package:" + pkg);
        Intent manage = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);

        manage.setData(pkgUri);/* w  w w  . ja va  2  s  .co m*/

        Intent whitelist = new Intent(ctxt, WhitelistReceiver.class);

        whitelist.setData(pkgUri);

        Intent main = new Intent(ctxt, MainActivity.class);

        main.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

        NotificationCompat.Builder b = new NotificationCompat.Builder(ctxt);
        String text = String.format(ctxt.getString(R.string.msg_requested),
                isReplace ? ctxt.getString(R.string.msg_upgraded) : ctxt.getString(R.string.msg_installed),
                pkg);

        b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).setWhen(System.currentTimeMillis())
                .setContentTitle(ctxt.getString(R.string.msg_detected)).setContentText(text)
                .setSmallIcon(android.R.drawable.stat_notify_error)
                .setTicker(ctxt.getString(R.string.msg_detected))
                .setContentIntent(PendingIntent.getActivity(ctxt, 0, manage, PendingIntent.FLAG_UPDATE_CURRENT))
                .addAction(R.drawable.ic_verified_user_24dp, ctxt.getString(R.string.msg_whitelist),
                        PendingIntent.getBroadcast(ctxt, 0, whitelist, 0))
                .addAction(R.drawable.ic_settings_24dp, ctxt.getString(R.string.msg_settings),
                        PendingIntent.getActivity(ctxt, 0, main, 0));

        NotificationManager mgr = (NotificationManager) ctxt.getSystemService(Context.NOTIFICATION_SERVICE);

        mgr.notify(NOTIFY_ID, b.build());
    }
}

From source file:cl.apd.ditapp.network.gcm.DitappGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param data GCM message received.//from   ww w .  ja va2  s.  com
 */
private void sendNotification(final Bundle data) {

    RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build();
    Realm realm = Realm.getInstance(realmConfig);
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            Notificacion notificacion = realm.createObject(Notificacion.class);
            notificacion.setTitulo(data.getString(Constants.NOTIFICACION_TITULO));
            notificacion.setDescripcion(data.getString(Constants.NOTIFICACION_DESCRIPCION));
            notificacion.setFecha(new Date());
        }
    });

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher).setContentTitle(data.getString(Constants.NOTIFICACION_TITULO))
            .setContentText("un mensaje").setAutoCancel(true).setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

From source file:br.ufrn.dimap.pubshare.download.service.DownloaderService.java

/**
* The IntentService calls this method from the default worker thread with
* the intent that started the service. When this method returns, IntentService
* stops the service, as appropriate.//  www. ja  v  a2 s.  co  m
*/
@Override
protected void onHandleIntent(Intent intent) {
    Log.d(TAG, "onHandleIntent on DownloadService");

    Article selectedArticle = (Article) intent.getSerializableExtra(Article.KEY_INSTANCE);

    if (!AndroidUtils.isExternalStorageAvailable()) {
        // Generate Menssages
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_menu_notifications)
                .setContentTitle(getResources().getString(R.string.external_storage_unavailable))
                .setContentText(getResources().getString(R.string.check_media_availability));
        Notification notification = mBuilder.build();
        // Set the Notification as ongoing
        notification.flags = notification.flags | Notification.FLAG_AUTO_CANCEL;

        NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nManager.notify(0, notification);
        return;
    }

    DownloadManager dowloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); // since API level 9

    Request request = new Request(Uri.parse(selectedArticle.getRemoteLocation()));
    request.setAllowedNetworkTypes(Request.NETWORK_WIFI | Request.NETWORK_MOBILE);
    request.setTitle(selectedArticle.getTitle());
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
            selectedArticle.generateFileName());
    request.setVisibleInDownloadsUi(true);

    long enqueue = dowloadManager.enqueue(request);

    Log.d(TAG, "Download enqueue..." + enqueue);

    ArticleDownloaded articleDownloaded = new ArticleDownloaded();
    articleDownloaded.setDownloadKey(enqueue);

    DownloadDao downloadDao = new DownloadDao(this);
    downloadDao.insert(articleDownloaded);

    Log.d(TAG, "Insert " + articleDownloaded + " in SqLite: OK");
}

From source file:com.alobha.challenger.business.gmc.NotificationGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param challenge GCM challenge received.
 *///from w ww .  j ava 2 s .c o  m
private void sendNotification(Challenge challenge) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("challenge_id", challenge.id);
    intent.setAction("ShowNotification");
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.inner_logo);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.inner_logo).setLargeIcon(largeIcon)
            .setContentTitle(getString(R.string.app_name))
            .setContentText("You were challenged by " + challenge.owner.first_name).setAutoCancel(true)
            .setSound(defaultSoundUri).setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

From source file:com.commonsware.android.andcorder.RecorderService.java

private void foregroundify(boolean showRecord) {
    NotificationCompat.Builder b = new NotificationCompat.Builder(this);

    b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);

    b.setContentTitle(getString(R.string.app_name)).setSmallIcon(R.mipmap.ic_launcher)
            .setTicker(getString(R.string.app_name));

    if (showRecord) {
        b.addAction(R.drawable.ic_videocam_white_24dp, getString(R.string.notify_record),
                buildPendingIntent(ACTION_RECORD));
    } else {/*from  ww  w.  j av a  2s .  c  om*/
        b.addAction(R.drawable.ic_stop_white_24dp, getString(R.string.notify_stop),
                buildPendingIntent(ACTION_STOP));
    }

    b.addAction(R.drawable.ic_eject_white_24dp, getString(R.string.notify_shutdown),
            buildPendingIntent(ACTION_SHUTDOWN));

    if (isForeground) {
        NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        mgr.notify(NOTIFY_ID, b.build());
    } else {
        startForeground(NOTIFY_ID, b.build());
        isForeground = true;
    }
}

From source file:cn.studyjams.s2.sj0119.NForget.AlarmReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    int mReceivedID = Integer.parseInt(intent.getStringExtra(ReminderEditActivity.EXTRA_REMINDER_ID));

    // Get notification title from Reminder Database
    ReminderDatabase rb = new ReminderDatabase(context);
    Reminder reminder = rb.getReminder(mReceivedID);
    String mTitle = reminder.getTitle();

    // Create intent to open ReminderEditActivity on notification click
    Intent editIntent = new Intent(context, ReminderEditActivity.class);
    editIntent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(mReceivedID));
    PendingIntent mClick = PendingIntent.getActivity(context, mReceivedID, editIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Create Notification
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
            .setSmallIcon(R.drawable.ic_alarm_on_white_24dp)
            .setContentTitle(context.getResources().getString(R.string.app_name)).setTicker(mTitle)
            .setContentText(mTitle).setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(mClick).setAutoCancel(true).setOnlyAlertOnce(true);

    NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nManager.notify(mReceivedID, mBuilder.build());
}

From source file:ca.justinrichard.link.MyFirebaseMessagingService.java

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 *///  ww w. j  av  a2  s . c  o  m
private void sendNotification(String messageBody, String linkId) {

    Intent intent;
    if (linkId.equals("")) {
        intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    } else {
        intent = new Intent(this, LinkActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(LINK_ID, linkId);
    }

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_link).setColor(getResources().getColor(R.color.colorPrimary))
            .setContentTitle("Link request").setContentText(messageBody).setAutoCancel(true)
            .setSound(defaultSoundUri).setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

From source file:com.anandroid.firebase.utils.MyFirebaseMessagingService.java

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 *//*  ww  w. j  a v a 2  s. c  o  m*/
private void sendNotification(String messageBody) {

    Intent intent;
    if (messageBody.indexOf("F") == 0) {
        intent = new Intent(this, Analytics.class);

    } else {
        intent = new Intent(this, MainActivity.class);

    }
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher).setContentTitle("FCM Message").setContentText(messageBody)
            .setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}