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:com.app.poke.poke.GcmIntentService.java

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

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivityPhone.class),
            0);/*from  ww  w .j a va  2  s .  c om*/

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.powered_by_google_dark).setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg);

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

From source file:com.NotifyMe.GcmIntentService.java

private void sendNotificationWithExtras(Bundle extras) {
    String message = extras.getString("message");
    String service = extras.getString("service");
    String type = extras.getString("type");
    String title = "NotifyMe [" + service + "]";

    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent;//from w  ww .  j a v a  2  s  . c o m
    if (service.equals("Reddit")) {
        Intent resultIntent = new Intent(Intent.ACTION_VIEW);
        String url = "http://reddit.com";
        if (type.equals("reddit-front-page")) {
            if (Integer.parseInt(extras.getString("count")) == 1) {
                try {
                    JSONArray posts = new JSONArray(extras.getString("links"));
                    url = posts.getJSONObject(0).getString("url");
                    message = posts.getJSONObject(0).getString("title");
                } catch (JSONException e) {
                    url = "http://reddit.com";
                }
            } else {
                url = "http://reddit.com";
            }
        } else if (type.equals("user-comment") || type.equals("user-submission")) {
            url = extras.getString("link");
        }
        resultIntent.setData(Uri.parse(url));
        contentIntent = PendingIntent.getActivity(this, 0, resultIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    } else if (service.equals("weather")) {
        Intent resultIntent;
        Intent weatherIntent = getWeatherAppIntent();
        if (weatherIntent != null) {
            resultIntent = weatherIntent;
        } else {
            resultIntent = new Intent(Intent.ACTION_VIEW);
            String url = "http://weather.com/";
            resultIntent.setData(Uri.parse(url));
        }
        contentIntent = PendingIntent.getActivity(this, 0, resultIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    } else if (service.equals("poly")) {
        Intent resultIntent = new Intent(Intent.ACTION_VIEW);
        String url = "https://www4.polymtl.ca/poly/poly.html";
        resultIntent.setData(Uri.parse(url));
        contentIntent = PendingIntent.getActivity(this, 0, resultIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    } else if (service.equals("github")) {
        Intent resultIntent = new Intent(Intent.ACTION_VIEW);
        String url = extras.getString("link");
        resultIntent.setData(Uri.parse(url));
        contentIntent = PendingIntent.getActivity(this, 0, resultIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    } else {
        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(title).setAutoCancel(true)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message)).setContentText(message)
            .setDefaults(Notification.DEFAULT_VIBRATE).setOnlyAlertOnce(true);

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

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.AppReferenceObj.java

@Override
public void handleDirectMessage(Context context, Contact from, JSONObject obj) {
    String packageName = obj.optString(PACKAGE_NAME);
    String arg = obj.optString(ARG);
    Intent launch = new Intent();
    launch.setAction(Intent.ACTION_MAIN);
    launch.addCategory(Intent.CATEGORY_LAUNCHER);
    launch.putExtra(AppState.EXTRA_APPLICATION_ARGUMENT, arg);
    launch.putExtra("creator", false);
    launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    launch.setPackage(packageName);/*from   w  w  w .ja v a  2  s .  com*/
    final PackageManager mgr = context.getPackageManager();
    List<ResolveInfo> resolved = mgr.queryIntentActivities(launch, 0);
    if (resolved == null || resolved.size() == 0) {
        Toast.makeText(context, "Could not find application to handle invite", Toast.LENGTH_SHORT).show();
        return;
    }
    ActivityInfo info = resolved.get(0).activityInfo;
    launch.setComponent(new ComponentName(info.packageName, info.name));
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, launch,
            PendingIntent.FLAG_CANCEL_CURRENT);

    (new PresenceAwareNotify(context)).notify("New Invitation", "Invitation received from " + from.name,
            "Click to launch application.", contentIntent);
}

From source file:de.tudarmstadt.dvs.myhealthassistant.myhealthhub.services.SystemMonitor.java

private int systemNotice() {
    int t = START_STICKY;
    Log.e(SystemMonitor.class.getSimpleName(), "call me redundant BABY!  onStartCommand service");

    int myID = android.os.Process.myPid();
    // The intent to launch when the user clicks the expanded notification
    Intent intent = new Intent(this, MyHealthHubWithFragments.class);
    // Intent intent = new Intent();
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);

    Notification notice = new Notification.Builder(getApplicationContext()).setSmallIcon(R.drawable.ic_launcher)
            .setWhen(System.currentTimeMillis()).setContentTitle(getPackageName())
            .setContentText("System Monitor running").setContentIntent(pendIntent).getNotification();

    notice.flags |= Notification.FLAG_AUTO_CANCEL;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(myID, notice);

    return t;/*from  w ww  .ja  v a2 s.com*/
}

From source file:com.commontime.plugin.notification.notification.Builder.java

/**
 * Set intent to handle the click event. Will bring the app to
 * foreground./*from   w  w w.  j  a  va 2s . co m*/
 *
 * @param builder
 *      Local notification builder instance
 */
private void applyContentReceiver(NotificationCompat.Builder builder) {

    if (clickActivity == null)
        return;

    Intent intent = new Intent(context, clickActivity).putExtra(Options.EXTRA, options.toString())
            .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

    int requestCode = new Random().nextInt();

    PendingIntent contentIntent = PendingIntent.getActivity(context, requestCode, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    builder.setContentIntent(contentIntent);
}

From source file:com.upnext.blekit.actions.BLEAction.java

/**
 * Displays a notification//from w w  w  .  j  a  v  a  2  s  .  c  om
 *
 * @param context Android Context, passed from the calling entity
 * @param title title for the notification
 * @param msg message and ticker for the notification
 * @param type action type that will be put as an extra into the result Intent (<code>resultIntent.putExtra("type", type);</code>)
 * @param notificationIconResId notification icon resource id
 * @param notificationId an identifier for this notification as in {@link android.app.NotificationManager#notify(int, android.app.Notification)}
 */
public static void displayNotification(Context context, String title, String msg, String type,
        int notificationIconResId, int notificationId) {
    L.d(".");

    if (BLEKit.getTargetActivityForNotifications() == null) {
        throw new IllegalArgumentException(
                "Target activity for notifications is not set. Call BLEKit.getTargetActivityForNotifications() first.");
    }

    Notification.Builder builder = new Notification.Builder(context).setContentText(msg).setTicker(msg)
            .setContentTitle(title).setAutoCancel(true).setSmallIcon(notificationIconResId);

    Intent resultIntent = new Intent(context, BLEKit.getTargetActivityForNotifications());
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    resultIntent.putExtra("type", type);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(resultPendingIntent);

    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(notificationId, builder.build());
}

From source file:com.chess.genesis.net.GenesisNotifier.java

private void SendNotification(final int id, final String text) {
    final Intent intent;
    if (Pref.getBool(this, R.array.pf_tabletMode)) {
        intent = new Intent(this, MainMenuTablet.class);

        final Bundle bundle = new Bundle();
        bundle.putInt("loadFrag", Enums.ONLINE_LIST);
        intent.putExtras(bundle);//from   www .  j  av  a  2  s.  co  m
    } else {
        intent = new Intent(this, GameListOnline.class);
    }

    final PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    final Notification note = createNotification(id, text, pintent);

    nm.notify(id, note);
}

From source file:de.evilbrain.sendtosftp.Main.java

private void notificationSend(String Title, String Text) {
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    NotificationCompat.Builder notificationbuilder = new NotificationCompat.Builder(this);
    Notification notification = notificationbuilder.setContentIntent(pIntent)
            .setSmallIcon(R.drawable.ic_launcher).setTicker(Title).setWhen(0).setAutoCancel(true)
            .setContentTitle(Title).setStyle(new NotificationCompat.BigTextStyle().bigText(Text))
            .setContentText(Text).build();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(notificationID, notification);
    notificationID++;/*from   ww w.  j  a  va 2 s.  c  o m*/
}

From source file:com.klinker.android.twitter.utils.NotificationUtils.java

public static void refreshNotification(Context context, boolean noTimeline) {
    AppSettings settings = AppSettings.getInstance(context);

    SharedPreferences sharedPrefs = context.getSharedPreferences(
            "com.klinker.android.twitter_world_preferences",
            Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
    int currentAccount = sharedPrefs.getInt("current_account", 1);

    //int[] unreadCounts = new int[] {4, 1, 2}; // for testing
    int[] unreadCounts = getUnreads(context);

    int timeline = unreadCounts[0];
    int realTimelineCount = timeline;

    // if they don't want that type of notification, simply set it to zero
    if (!settings.timelineNot || (settings.pushNotifications && settings.liveStreaming) || noTimeline) {
        unreadCounts[0] = 0;//from  w w w .  ja v  a  2 s .c o  m
    }
    if (!settings.mentionsNot) {
        unreadCounts[1] = 0;
    }
    if (!settings.dmsNot) {
        unreadCounts[2] = 0;
    }

    if (unreadCounts[0] == 0 && unreadCounts[1] == 0 && unreadCounts[2] == 0) {

    } else {
        Intent markRead = new Intent(context, MarkReadService.class);
        PendingIntent readPending = PendingIntent.getService(context, 0, markRead, 0);

        String shortText = getShortText(unreadCounts, context, currentAccount);
        String longText = getLongText(unreadCounts, context, currentAccount);
        // [0] is the full title and [1] is the screenname
        String[] title = getTitle(unreadCounts, context, currentAccount);
        boolean useExpanded = useExp(context);
        boolean addButton = addBtn(unreadCounts);

        if (title == null) {
            return;
        }

        Intent resultIntent;

        if (unreadCounts[1] != 0 && unreadCounts[0] == 0) {
            // it is a mention notification (could also have a direct message)
            resultIntent = new Intent(context, RedirectToMentions.class);
        } else if (unreadCounts[2] != 0 && unreadCounts[0] == 0 && unreadCounts[1] == 0) {
            // it is a direct message
            resultIntent = new Intent(context, RedirectToDMs.class);
        } else {
            resultIntent = new Intent(context, MainActivity.class);
        }

        PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);

        NotificationCompat.Builder mBuilder;

        Intent deleteIntent = new Intent(context, NotificationDeleteReceiverOne.class);

        mBuilder = new NotificationCompat.Builder(context).setContentTitle(title[0])
                .setContentText(TweetLinkUtils.removeColorHtml(shortText, settings))
                .setSmallIcon(R.drawable.ic_stat_icon).setLargeIcon(getIcon(context, unreadCounts, title[1]))
                .setContentIntent(resultPendingIntent).setAutoCancel(true)
                .setTicker(TweetLinkUtils.removeColorHtml(shortText, settings))
                .setDeleteIntent(PendingIntent.getBroadcast(context, 0, deleteIntent, 0))
                .setPriority(NotificationCompat.PRIORITY_HIGH);

        if (unreadCounts[1] > 1 && unreadCounts[0] == 0 && unreadCounts[2] == 0) {
            // inbox style notification for mentions
            mBuilder.setStyle(getMentionsInboxStyle(unreadCounts[1], currentAccount, context,
                    TweetLinkUtils.removeColorHtml(shortText, settings)));
        } else if (unreadCounts[2] > 1 && unreadCounts[0] == 0 && unreadCounts[1] == 0) {
            // inbox style notification for direct messages
            mBuilder.setStyle(getDMInboxStyle(unreadCounts[1], currentAccount, context,
                    TweetLinkUtils.removeColorHtml(shortText, settings)));
        } else {
            // big text style for an unread count on timeline, mentions, and direct messages
            mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(Html.fromHtml(
                    settings.addonTheme ? longText.replaceAll("FF8800", settings.accentColor) : longText)));
        }

        // Pebble notification
        if (sharedPrefs.getBoolean("pebble_notification", false)) {
            sendAlertToPebble(context, title[0], shortText);
        }

        // Light Flow notification
        sendToLightFlow(context, title[0], shortText);

        int homeTweets = unreadCounts[0];
        int mentionsTweets = unreadCounts[1];
        int dmTweets = unreadCounts[2];

        int newC = 0;

        if (homeTweets > 0) {
            newC++;
        }
        if (mentionsTweets > 0) {
            newC++;
        }
        if (dmTweets > 0) {
            newC++;
        }

        if (settings.notifications && newC > 0) {

            if (settings.vibrate) {
                mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
            }

            if (settings.sound) {
                try {
                    mBuilder.setSound(Uri.parse(settings.ringtone));
                } catch (Exception e) {
                    mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
                }
            }

            if (settings.led)
                mBuilder.setLights(0xFFFFFF, 1000, 1000);

            // Get an instance of the NotificationManager service
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

            if (addButton) { // the reply and read button should be shown
                Intent reply;
                if (unreadCounts[1] == 1) {
                    reply = new Intent(context, NotificationCompose.class);
                } else {
                    reply = new Intent(context, NotificationDMCompose.class);
                }

                Log.v("username_for_noti", title[1]);
                sharedPrefs.edit().putString("from_notification", "@" + title[1] + " " + title[2]).commit();
                MentionsDataSource data = MentionsDataSource.getInstance(context);
                long id = data.getLastIds(currentAccount)[0];
                PendingIntent replyPending = PendingIntent.getActivity(context, 0, reply, 0);
                sharedPrefs.edit().putLong("from_notification_long", id).commit();
                sharedPrefs.edit()
                        .putString("from_notification_text",
                                "@" + title[1] + ": " + TweetLinkUtils.removeColorHtml(shortText, settings))
                        .commit();

                // Create the remote input
                RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
                        .setLabel("@" + title[1] + " ").build();

                // Create the notification action
                NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
                        R.drawable.ic_action_reply_dark, context.getResources().getString(R.string.noti_reply),
                        replyPending).addRemoteInput(remoteInput).build();

                NotificationCompat.Action.Builder action = new NotificationCompat.Action.Builder(
                        R.drawable.ic_action_read_dark, context.getResources().getString(R.string.mark_read),
                        readPending);

                mBuilder.addAction(replyAction);
                mBuilder.addAction(action.build());
            } else { // otherwise, if they can use the expanded notifications, the popup button will be shown
                Intent popup = new Intent(context, RedirectToPopup.class);
                popup.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                popup.putExtra("from_notification", true);

                PendingIntent popupPending = PendingIntent.getActivity(context, 0, popup, 0);

                NotificationCompat.Action.Builder action = new NotificationCompat.Action.Builder(
                        R.drawable.ic_popup, context.getResources().getString(R.string.popup), popupPending);

                mBuilder.addAction(action.build());
            }

            // Build the notification and issues it with notification manager.
            notificationManager.notify(1, mBuilder.build());

            // if we want to wake the screen on a new message
            if (settings.wakeScreen) {
                PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                final PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK
                        | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
                wakeLock.acquire(5000);
            }
        }

        // if there are unread tweets on the timeline, check them for favorite users
        if (settings.favoriteUserNotifications && realTimelineCount > 0) {
            favUsersNotification(currentAccount, context);
        }
    }

    try {

        ContentValues cv = new ContentValues();

        cv.put("tag", "com.klinker.android.twitter/com.klinker.android.twitter.ui.MainActivity");

        // add the direct messages and mentions
        cv.put("count", unreadCounts[1] + unreadCounts[2]);

        context.getContentResolver().insert(Uri.parse("content://com.teslacoilsw.notifier/unread_count"), cv);

    } catch (IllegalArgumentException ex) {

        /* Fine, TeslaUnread is not installed. */

    } catch (Exception ex) {

        /* Some other error, possibly because the format
           of the ContentValues are incorrect.
                
        Log but do not crash over this. */

        ex.printStackTrace();

    }
}