Example usage for android.content Context NOTIFICATION_SERVICE

List of usage examples for android.content Context NOTIFICATION_SERVICE

Introduction

In this page you can find the example usage for android.content Context NOTIFICATION_SERVICE.

Prototype

String NOTIFICATION_SERVICE

To view the source code for android.content Context NOTIFICATION_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.app.NotificationManager for informing the user of background events.

Usage

From source file:hk.edu.cityu.appslab.calmessenger.gcm.GcmIntentService.java

protected void sendNotification(String title, String msg, Intent intent) {
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent;/* w w  w . java2s.c  om*/
    if (intent == null) {
        contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ConversationActivity.class), 0);
    } else {
        contentIntent = TaskStackBuilder.create(this).addNextIntentWithParentStack(intent).getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);
        // contentIntent = PendingIntent.getActivity(this, 0, intent,
        // PendingIntent.FLAG_CANCEL_CURRENT);
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher).setContentTitle(title)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg)
            .setAutoCancel(true);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

From source file:com.granita.contacticloudsync.syncadapter.AccountSettings.java

@TargetApi(21)
protected void showNotification(int id, String title, String message) {
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder n = new Notification.Builder(context);
    if (Build.VERSION.SDK_INT >= 16) {
        n.setPriority(Notification.PRIORITY_HIGH);
        n.setStyle(new Notification.BigTextStyle().bigText(message));
    }//from   w w  w .  jav a  2 s. co m
    if (Build.VERSION.SDK_INT >= 20)
        n.setLocalOnly(true);
    if (Build.VERSION.SDK_INT >= 21)
        n.setCategory(Notification.CATEGORY_SYSTEM);
    n.setSmallIcon(R.drawable.ic_launcher);
    n.setContentTitle(title);
    n.setContentText(message);
    nm.notify(id, Build.VERSION.SDK_INT >= 16 ? n.build() : n.getNotification());
}

From source file:addresspager.test_google_cloud_messaging.GcmIntentService.java

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)
            .setSmallIcon(R.drawable.ic_stat_gcm).setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

From source file:com.ad.mediasharing.ADUploadMediaTask.java

public ADUploadMediaTask(Activity activity, ADMediaShareRequest request,
        ADRequestCallbackHandler<String, Exception, Integer> callbackHandler,
        boolean isPreferenceProgressEnabled, boolean isProgressBarEnabled) {

    super();// w  w  w.  j av  a2 s.  co  m

    this.mActivity = activity;
    this.mRequest = request;
    this.mCallbackHandler = callbackHandler;
    this.isPreferenceProgressEnabled = isPreferenceProgressEnabled;
    this.isProgressBarEnabled = isProgressBarEnabled;

    if (activity != null) {
        notificationManager = (NotificationManager) mActivity.getApplicationContext()
                .getSystemService(Context.NOTIFICATION_SERVICE);
    }
}

From source file:br.com.awa.mylottery.gcm.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received./* ww w  . j  av a2s .c o m*/
 */
private void sendNotification(String message) {
    Intent intent = new Intent(this, HomeActivity.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.common_ic_googleplayservices).setContentTitle("MyLottery")
            .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.raspi.chatapp.util.Notification.java

public void createNotification(String buddyId, String name, String message, String type) {
    SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

    if (defaultSharedPreferences
            .getBoolean(context.getResources().getString(R.string.pref_key_new_message_notifications), true)) {
        int index = buddyId.indexOf("@");
        if (index != -1)
            buddyId = buddyId.substring(0, index);
        if (name == null)
            name = buddyId;//from   w w w .  j  a  v a  2 s .  c  o m
        Log.d("DEBUG", "creating notification: " + buddyId + "|" + name + "|" + message);
        Intent resultIntent = new Intent(context, ChatActivity.class);
        resultIntent.setAction(NOTIFICATION_CLICK);
        String oldBuddyId = getOldBuddyId();
        Log.d("DEBUG",
                (oldBuddyId == null) ? ("oldBuddy is null (later " + buddyId) : ("oldBuddy: " + oldBuddyId));
        if (oldBuddyId == null || oldBuddyId.equals("")) {
            oldBuddyId = buddyId;
            setOldBuddyId(buddyId);
        }
        if (oldBuddyId.equals(buddyId)) {
            resultIntent.putExtra(Constants.BUDDY_ID, buddyId);
            resultIntent.putExtra(Constants.CHAT_NAME, name);
        }

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(ChatActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationManager nm = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
        NotificationCompat.Style style;

        String[] previousNotifications = readJSONArray(CURRENT_NOTIFICATIONS);
        String title;
        String[] currentNotifications = Arrays.copyOf(previousNotifications, previousNotifications.length + 1);
        currentNotifications[currentNotifications.length - 1] = name + ": " + message;
        if (previousNotifications.length == 0) {
            style = new NotificationCompat.BigTextStyle();
            NotificationCompat.BigTextStyle bigTextStyle = ((NotificationCompat.BigTextStyle) style);
            title = context.getResources().getString(R.string.new_message) + " "
                    + context.getResources().getString(R.string.from) + " " + name;
            bigTextStyle.bigText(currentNotifications[0]);
            bigTextStyle.setBigContentTitle(title);
        } else {
            style = new NotificationCompat.InboxStyle();
            NotificationCompat.InboxStyle inboxStyle = (NotificationCompat.InboxStyle) style;
            title = (previousNotifications.length + 1) + " "
                    + context.getResources().getString(R.string.new_messages);
            for (String s : currentNotifications)
                if (s != null && !"".equals(s))
                    inboxStyle.addLine(s);
            inboxStyle.setSummaryText(
                    (currentNotifications.length > 2) ? ("+" + (currentNotifications.length - 2) + " more")
                            : null);
            inboxStyle.setBigContentTitle(title);
        }
        writeJSONArray(currentNotifications, CURRENT_NOTIFICATIONS);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setContentTitle(title)
                .setContentText(currentNotifications[currentNotifications.length - 1])
                .setSmallIcon(MessageHistory.TYPE_TEXT.equals(type) ? R.drawable.ic_forum_white_48dp
                        : R.drawable.ic_photo_camera_white_48dp)
                .setLargeIcon(getLargeIcon(Character.toUpperCase(name.toCharArray()[0]))).setStyle(style)
                .setAutoCancel(true).setPriority(5).setContentIntent(resultPendingIntent);

        String str = context.getResources().getString(R.string.pref_key_privacy);
        mBuilder.setVisibility(context.getResources().getString(R.string.pref_value1_privacy).equals(str)
                ? NotificationCompat.VISIBILITY_SECRET
                : context.getResources().getString(R.string.pref_value2_privacy).equals(str)
                        ? NotificationCompat.VISIBILITY_PRIVATE
                        : NotificationCompat.VISIBILITY_PUBLIC);

        str = context.getResources().getString(R.string.pref_key_vibrate);
        if (defaultSharedPreferences.getBoolean(str, true))
            mBuilder.setVibrate(new long[] { 800, 500, 800, 500 });

        str = context.getResources().getString(R.string.pref_key_led);
        if (defaultSharedPreferences.getBoolean(str, true))
            mBuilder.setLights(Color.BLUE, 500, 500);

        str = defaultSharedPreferences.getString(context.getResources().getString(R.string.pref_key_ringtone),
                "");
        mBuilder.setSound("".equals(str) ? RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
                : Uri.parse(str));

        nm.notify(NOTIFICATION_ID, mBuilder.build());
        str = context.getResources().getString(R.string.pref_key_banner);
        if (!defaultSharedPreferences.getBoolean(str, true)) {
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
            }
            reset();
        }
    }
}

From source file:com.example.kyle.weatherforecast.MainActivity.java

private void postAlert(int i) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle("Weather Alert!").setContentText(WeatherData.outlookArray[i])
            .setSmallIcon(R.drawable.small_icon)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), WeatherData.symbolArray[i]))
            .setAutoCancel(true).setTicker("Wrap up warm!")
            // Heads-up and lock screen notifications
            .setVisibility(Notification.VISIBILITY_PUBLIC).setPriority(Notification.PRIORITY_HIGH)
            .setVibrate(new long[] { 100, 100, 100, 200, 200 }).setVibrate(new long[] { 0 })
            .setCategory(Notification.CATEGORY_ALARM);

    NotificationCompat.BigPictureStyle bigStyle = new NotificationCompat.BigPictureStyle();
    bigStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.snow_scene));
    builder.setStyle(bigStyle);//  w  w w  . j  ava2  s  .c o m

    Intent intent = new Intent(this, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class).addNextIntent(intent);
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(notificationId, builder.build());

    notificationId++;
    Log.d(DEBUG_TAG, "ID: " + notificationId);
}

From source file:com.krayzk9s.imgurholo.services.UploadService.java

public void onGetObject(Object o, String tag) {
    String id = (String) o;
    if (id.length() == 7) {
        if (totalUpload != -1)
            ids.add(id);//  w  w w. j  av a2  s  .co  m
        if (ids.size() == totalUpload) {
            ids.add(0, ""); //weird hack because imgur eats the first item of the array for some bizarre reason
            NewAlbumAsync newAlbumAsync = new NewAlbumAsync("", "", apiCall, ids, this);
            newAlbumAsync.execute();
        }
    } else if (apiCall.settings.getBoolean("AlbumUpload", true)) {
        NotificationManager notificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        Intent viewImageIntent = new Intent();
        viewImageIntent.setAction(Intent.ACTION_VIEW);
        viewImageIntent.setData(Uri.parse("http://imgur.com/a/" + id));
        Intent shareIntent = new Intent();
        shareIntent.setType("text/plain");
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        shareIntent.putExtra(Intent.EXTRA_TEXT, "http://imgur.com/a/" + id);
        PendingIntent viewImagePendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(),
                viewImageIntent, 0);
        PendingIntent sharePendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(),
                shareIntent, 0);
        Notification notification = notificationBuilder.setSmallIcon(R.drawable.icon_desaturated)
                .setContentText("Finished Uploading Album").setContentTitle("imgur Image Uploader")
                .setContentIntent(viewImagePendingIntent)
                .addAction(R.drawable.dark_social_share, "Share", sharePendingIntent).build();
        Log.d("Built", "Notification built");
        notificationManager.cancel(0);
        notificationManager.notify(1, notification);
        Log.d("Built", "Notification display");
    } else {
        NotificationManager notificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        Notification notification = notificationBuilder.setSmallIcon(R.drawable.icon_desaturated)
                .setContentText("Finished Uploading All Images").setContentTitle("imgur Image Uploader")
                .build();
        Log.d("Built", "Notification built");
        notificationManager.cancel(0);
        notificationManager.notify(1, notification);
        Log.d("Built", "Notification display");
    }
}

From source file:uk.bowdlerize.service.CensorCensusService.java

@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
    if (null == mNotifyManager)
        mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (null == mBuilder)
        mBuilder = new NotificationCompat.Builder(this);

    if (null == api)
        api = new API(this);

    mContext = this;

    checkedCount = getPreferences(this).getInt("checkedCount", 0);
    censoredCount = getPreferences(this).getInt("censoredCount", 0);
    sendtoORG = getPreferences(this).getBoolean("sendToOrg", false);

    //Make it so tapping an intent will launch the app (Fix #12)
    launchAppIntent = new Intent(mContext, MainActivity.class);
    pendingIntent = PendingIntent.getActivity(mContext, 0, launchAppIntent, 0);
    mBuilder.setContentIntent(pendingIntent);

    //Lets findout why we've been started
    if (intent.getBooleanExtra(API.EXTRA_POLL, false) || intent.getBooleanExtra(API.EXTRA_GCM_TICKLE, false)) {
        prepProbe(intent);//from   w ww.j a  va  2  s.c om
    } else if (intent.hasExtra("url") && !intent.getStringExtra("url").equals("")) {
        performProbe(intent);
    } else {
        onProbeFinish();
    }

    //If we're polling we probably want to stay alive
    if (getSharedPreferences(MainActivity.class.getSimpleName(), Context.MODE_PRIVATE)
            .getInt(API.SETTINGS_GCM_PREFERENCE, API.SETTINGS_GCM_FULL) == API.SETTINGS_GCM_DISABLED) {
        return START_STICKY;
    } else {
        return START_NOT_STICKY;
    }
}

From source file:app.taxi.best.bestofthebesttaxiapp.gcm.GcmIntentService.java

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)
            .setSmallIcon(R.drawable.ic_plusone_small_off_client).setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}