Example usage for android.app PendingIntent getActivity

List of usage examples for android.app PendingIntent getActivity

Introduction

In this page you can find the example usage for android.app PendingIntent getActivity.

Prototype

public static PendingIntent getActivity(Context context, int requestCode, Intent intent, @Flags int flags) 

Source Link

Document

Retrieve a PendingIntent that will start a new activity, like calling Context#startActivity(Intent) Context.startActivity(Intent) .

Usage

From source file:au.com.websitemasters.schools.lcps.push.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received./* w  w  w.  j a v  a 2s. c  o m*/
 */

public void sendNotification(String message, Class clas) {

    //load not readed. +1. save em.
    int notReaded = ((SchoolsApplication) getApplicationContext()).loadBadgesCount();
    notReaded++;
    ((SchoolsApplication) getApplicationContext()).saveBadgesCount(notReaded);

    //show it on badge.
    ShortcutBadger.applyCount(getApplicationContext(), notReaded);

    Intent intent = new Intent(this, clas);
    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.logo_gray).setContentTitle("Leschenault Catholic Primary School")
            .setContentText(message).setAutoCancel(true).setNumber(notReaded).setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

    //push realtime refresh of lists (ANN)
    Intent intentBroadcast = new Intent(BROADCAST_ACTION);
    sendBroadcast(intentBroadcast);
}

From source file:de.taxilof.UulmLoginAgent.java

/**
 * notify the User in Statusbar//  w ww .j  a  v  a  2s .c  om
 */
private void notify(String subject, String message, boolean errorIcon) {
    // build notification with notifyString
    Notification notifyDetails;
    mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (errorIcon) {
        notifyDetails = new Notification(R.drawable.icon_red, subject, System.currentTimeMillis());
    } else {
        notifyDetails = new Notification(R.drawable.icon, subject, System.currentTimeMillis());
    }
    PendingIntent myIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
    notifyDetails.setLatestEventInfo(context, subject, message, myIntent);
    notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
    mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
}

From source file:edu.mines.letschat.GcmIntentService.java

private void sendNotification(String msg, String senderID) {
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra(MainActivity.EXTRA_NOTIFICATION_RETRIEVE, senderID);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    notificationCounter++;/*  w  ww  . j a v a 2 s .  co m*/

    if (messages.size() > 1) {
        msg = msg + "...";
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setVibrate(new long[] { 0, 500, 250, 500, 250 }).setLights(Color.BLUE, 200, 200)
            .setSmallIcon(R.drawable.logo).setContentTitle("Let's Chat Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setNumber(notificationCounter)
            .setTicker(msg).setContentText(msg);

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    // Sets a title for the Inbox style big view
    inboxStyle.setBigContentTitle("Unread messages (" + notificationCounter + "):");
    // Moves events into the big view
    ArrayList<String> temp = new ArrayList<String>();
    for (int i = 0; i < 5; ++i) {
        if (i == messages.size()) {
            break;
        }
        temp.add(messages.get(i));
    }
    Collections.reverse(temp);
    messages = temp;
    for (String s : messages) {
        inboxStyle.addLine(s);
    }

    mBuilder.setStyle(inboxStyle);

    mBuilder.setAutoCancel(true);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    //        AwesomeAdapter.animate = true;
}

From source file:com.nanostuffs.yurdriver.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//*from  w w w .  j a  va 2s  . c  o  m*/
private void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, MapActivity.class);
    notificationIntent.putExtra("fromNotification", "notification");
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    System.out.println("notification====>" + message);
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    // notification.defaults |= Notification.DEFAULT_LIGHTS;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.ledARGB = 0x00000000;
    notification.ledOnMS = 0;
    notification.ledOffMS = 0;
    notificationManager.notify(AndyConstants.NOTIFICATION_ID, notification);
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = pm.newWakeLock(
            PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
            "WakeLock");
    wakeLock.acquire();
    wakeLock.release();

}

From source file:com.hybris.mobile.lib.location.geofencing.service.GeofencingIntentService.java

/**
 * Send a notification when a geofence is triggered
 *
 * @param geofence           the geofence triggered
 * @param notification       the notification object
 * @param geofenceTransition the geofence transition type
 *//*w  ww  .ja  v a  2s .  c o m*/
protected void sendNotification(Geofence geofence, GeofenceObject.Notification notification,
        int geofenceTransition) {

    if (notification != null) {

        // Notification
        String notificationContentTitle = notification.getNotificationTitle();
        String notificationContentText = notification.getNotificationText();
        int notificationIconResId = notification.getNotificationIconResId();

        Notification.Builder builder = new Notification.Builder(this);

        builder.setContentTitle(notificationContentTitle).setContentText(notificationContentText);

        if (notificationIconResId > 0) {
            builder.setSmallIcon(notificationIconResId);
        }

        try {
            // Intent on click on the notification
            if (StringUtils.isNotBlank(notification.getIntentClassDestination())) {
                Class<?> intentClassDestination = Class.forName(notification.getIntentClassDestination());

                // Create an explicit content Intent that starts the Activity defined in intentClassDestination
                Intent notificationIntent = new Intent(this, intentClassDestination);

                // Geofence Id to pass to the activity in order to retrieve the object
                if (notification.getIntentBundle() != null) {
                    GeofenceObject.IntentBundle intentBundle = notification.getIntentBundle();
                    notificationIntent.putExtra(intentBundle.getKeyName(), intentBundle.getBundle());

                    // Easter egg :)
                    if (intentBundle.getBundle().getBoolean(GeofencingConstants.EXTRA_PLAY_SOUND)) {
                        MediaPlayer mediaPlayer;
                        if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
                            Log.d(TAG, "Playing entering geofence sound");
                            mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.entering_geofence);
                        } else {
                            Log.d(TAG, "Playing exiting geofence sound");
                            mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.leaving_geofence);
                        }

                        mediaPlayer.start();
                    }
                }

                PendingIntent notificationPendingIntent = PendingIntent.getActivity(this,
                        geofence.getRequestId().hashCode(), notificationIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

                builder.setContentIntent(notificationPendingIntent);
            }

        } catch (ClassNotFoundException e) {
            Log.e(TAG, "Unable to find class " + notification.getIntentClassDestination() + "."
                    + e.getLocalizedMessage());
        }

        // Constructing the Notification and setting the flag to auto remove the notification when the user click on it
        Notification notificationView;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            notificationView = builder.build();
        } else {
            notificationView = builder.getNotification();
        }

        notificationView.flags = Notification.FLAG_AUTO_CANCEL;
        notificationView.defaults = Notification.DEFAULT_ALL;

        // Get an instance of the Notification manager
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);

        // Issue the notification
        mNotificationManager.notify(UUID.randomUUID().toString().hashCode(), notificationView);
    } else {
        Log.e(TAG, "Notification empty for Geofence " + geofence);
    }

}

From source file:com.mutu.gpstracker.streaming.CustomUpload.java

private void notifyError(Context context, Exception e) {
    Log.e(TAG, "Custom upload failed", e);
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);

    int icon = R.drawable.ic_maps_indicator_current_position;
    CharSequence tickerText = context.getText(R.string.customupload_failed);
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, when);

    Context appContext = context.getApplicationContext();
    CharSequence contentTitle = tickerText;
    CharSequence contentText = e.getMessage();
    Intent notificationIntent = new Intent(context, CustomUpload.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(appContext, contentTitle, contentText, contentIntent);
    notification.flags = Notification.FLAG_AUTO_CANCEL;

    mNotificationManager.notify(NOTIFICATION_ID, notification);
}

From source file:au.com.websitemasters.schools.thornlie.push.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.//from   w w  w  .  j  a v a  2 s. com
 */

private void sendNotification(String message, Class clas) {

    //load not readed. +1. save em.
    int notReaded = ((SchoolsApplication) getApplicationContext()).loadBadgesCount();
    notReaded = notReaded + 1;
    ((SchoolsApplication) getApplicationContext()).saveBadgesCount(notReaded);

    //show it on badge.
    ShortcutBadger.applyCount(getApplicationContext(), notReaded);

    Intent intent = new Intent(this, clas);
    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.logopush).setContentTitle("Sacred Heart School Thornlie")
            .setContentText(message).setAutoCancel(true).setNumber(notReaded).setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

    //push realtime refresh of lists (ANN)
    Intent intentBroadcast = new Intent(BROADCAST_ACTION);
    sendBroadcast(intentBroadcast);
}

From source file:com.sftoolworks.nfcoptions.SelectActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select);

    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    Intent intent = getIntent();/*from ww w. j  ava2s .  co m*/
    Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

    String title = getString(R.string.default_select_title);

    TextView textViewBottom = (TextView) findViewById(R.id.textView2);
    textViewBottom.setText("");

    if (rawMessages != null) {

        ArrayList<Object> entries = new ArrayList<Object>();

        String data = null;

        for (Parcelable rawMessage : rawMessages) {
            NdefMessage message = (NdefMessage) rawMessage;
            NdefRecord[] records = message.getRecords();
            if (records.length > 1) {
                byte[] bArray = records[1].getPayload();
                byte languageLength = bArray[0];

                languageLength++; // because of the length byte

                data = new String(bArray, languageLength, bArray.length - languageLength);
            }
        }

        try {
            JSONObject json = (JSONObject) new JSONTokener(data).nextValue();
            if (json.has("title"))
                title = json.getString("title");
            if (json.has("key"))
                selectKey = json.getString("key");
            if (json.has("options")) {
                JSONArray arr = json.getJSONArray("options");
                for (int i = 0; i < arr.length(); i++) {
                    entries.add(parseJObject(arr.getJSONObject(i)));
                }
            }

            ListView list = (ListView) findViewById(R.id.listView1);
            list.setAdapter(new OptionListAdapter(this, entries.toArray()));

            list.setOnItemClickListener(new ListView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int index, long id) {
                    CheckBox cb = (CheckBox) view.findViewById(R.id.optionListCheckBox);
                    cb.setChecked(!cb.isChecked());
                }
            });

            textViewBottom.setText(getString(R.string.tap_again));

        } catch (Exception e) {
            String message = getString(R.string.json_err);
            message += "\n";
            message += e.getMessage();

            Toast.makeText(this, message, Toast.LENGTH_LONG).show();

            Log.d(TAG, getString(R.string.json_err));
            Log.d(TAG, e.toString());
            Log.d(TAG, data);
        }
    } else {
        title = getString(R.string.no_tag);
    }

    TextView msg = (TextView) findViewById(R.id.textView1);
    msg.setText(title);

}

From source file:android.romstats.ReportingService.java

private void promptUser() {
    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    Intent mainActivity = new Intent(getApplicationContext(), AnonymousStats.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, mainActivity, 0);

    Notification notification = new NotificationCompat.Builder(getBaseContext())
            .setSmallIcon(R.drawable.ic_launcher).setTicker(getString(R.string.notification_ticker))
            .setContentTitle(getString(R.string.notification_title))
            .setContentText(getString(R.string.notification_desc)).setWhen(System.currentTimeMillis())
            .setContentIntent(pendingIntent).setAutoCancel(true).build();

    nm.notify(Utilities.NOTIFICATION_ID, notification);
}

From source file:org.transdroid.service.UpdateService.java

private void newNotification(String ticker, String title, String text, String downloadUrl, int notifyID) {

    // Use the alarm service settings for the notification sound/vibrate/colour
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    AlarmSettings settings = Preferences.readAlarmSettings(prefs);

    // Set up an intent that will initiate a download of the new version
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(downloadUrl));

    // Create a new notification
    Notification newNotification = new Notification(R.drawable.icon_notification, ticker,
            System.currentTimeMillis());
    newNotification.flags = Notification.FLAG_AUTO_CANCEL;
    newNotification.setLatestEventInfo(getApplicationContext(), title, text,
            PendingIntent.getActivity(getApplicationContext(), notifyID, i, 0));

    // Get the system notification manager, if not done so previously
    if (notificationManager == null) {
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }//from   w w  w  . j  ava2  s . c o  m

    // If sound enabled add to notification
    if (settings.getAlarmPlaySound() && settings.getAlarmSoundURI() != null) {
        newNotification.sound = Uri.parse(settings.getAlarmSoundURI());
    }

    // If vibration enabled add to notification
    if (settings.getAlarmVibrate()) {
        newNotification.defaults = Notification.DEFAULT_VIBRATE;
    }

    // Add coloured light; defaults to 0xff7dbb21
    newNotification.ledARGB = settings.getAlarmColour();
    newNotification.ledOnMS = 600;
    newNotification.ledOffMS = 1000;
    newNotification.flags |= Notification.FLAG_SHOW_LIGHTS;

    // Send notification
    notificationManager.notify(notifyID, newNotification);

}