Example usage for android.app PendingIntent FLAG_ONE_SHOT

List of usage examples for android.app PendingIntent FLAG_ONE_SHOT

Introduction

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

Prototype

int FLAG_ONE_SHOT

To view the source code for android.app PendingIntent FLAG_ONE_SHOT.

Click Source Link

Document

Flag indicating that this PendingIntent can be used only once.

Usage

From source file:com.piggeh.palmettoscholars.services.MyFirebaseMessagingService.java

public void notifyAnnouncement(String announcement) {
    Intent contentIntent = new Intent(this, MainActivity.class);
    contentIntent.putExtra("navigation_page", MainActivity.PAGE_ANNOUNCEMENTS);
    contentIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent contentPendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, contentIntent,
            PendingIntent.FLAG_ONE_SHOT);

    Intent settingsIntent = new Intent(this, SettingsActivity.class);
    settingsIntent.putExtra("navigation_page", MainActivity.PAGE_SETTINGS);
    settingsIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent settingsPendingIntent = PendingIntent.getActivity(this, 1 /* Request code */, settingsIntent,
            PendingIntent.FLAG_ONE_SHOT);

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

    NotificationCompat.BigTextStyle notifStyle = new NotificationCompat.BigTextStyle();
    notifStyle.bigText(announcement);//from   ww  w . ja  va  2s  .  c  o m
    notifStyle.setBigContentTitle(getString(R.string.notif_announcement_title));

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon_nodpi)
            .setContentTitle(getString(R.string.notif_announcement_title)).setContentText(announcement)
            .setAutoCancel(true).setSound(defaultSoundUri)
            .setColor(ContextCompat.getColor(this, R.color.colorPrimary)).setContentIntent(contentPendingIntent)
            .setStyle(notifStyle).addAction(R.drawable.ic_notifications_off,
                    getString(R.string.notif_action_options), settingsPendingIntent);

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

    // Build the notification and issues it with notification manager.
    notificationManager.notify(NOTIFICATION_ID_ANNOUNCEMENT, notificationBuilder.build());
}

From source file:com.getmarco.weatherstationviewer.gcm.StationGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.//from  w w  w .ja  v a2 s  . c o m
 */
private void sendNotification(String message) {
    Intent intent = new Intent(this, StationListActivity.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("Station update").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:kr.ds.recipe.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.//ww  w . j  a va2 s .  c om
 */
private void sendNotification(String message) {
    Intent intent = new Intent(this, IntroActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder;
    if (isvibrate) {
        notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle(getResources().getString(R.string.app_name)).setSmallIcon(R.mipmap.push_icon)
                .setContentText(message).setAutoCancel(true).setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
    } else {
        notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle(getResources().getString(R.string.app_name)).setSmallIcon(R.mipmap.push_icon)
                .setContentText(message).setAutoCancel(true).setVibrate(new long[] { 0 })
                .setContentIntent(pendingIntent);
    }

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

    notificationManager.notify(UniqueID.getRandomNumber(999), notificationBuilder.build());
}

From source file:com.preguardia.app.notification.MyGcmListenerService.java

private void showMessageNewNotification(String title, String message, String consultationId) {
    // Prepare intent which is triggered if the notification is selected
    Intent intent = new Intent(this, ConsultationDetailsActivity.class);

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra(Constants.EXTRA_CONSULTATION_ID, consultationId);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, Constants.GENERAL_NEW_MESSAGE_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_stat_logo).setContentTitle(title).setContentText(message)
            .setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent);

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

    notificationManager.notify(Constants.NOTIFICATION_NEW_MESSAGE_ID, notificationBuilder.build());
}

From source file:com.gsma.rcs.ri.messaging.filetransfer.FileTransferIntentService.java

/**
 * Forward file transfer invitation to UI
 * //from w  w w . j  a  v  a2  s  .  com
 * @param invitation Intent invitation
 * @param ftDao the file transfer data object
 */
private void forwardFileTransferInvitationToUi(Intent invitation, FileTransferDAO ftDao) {
    ContactId contact = ftDao.getContact();
    if (ftDao.getContact() == null) {
        if (LogUtils.isActive) {
            Log.e(LOGTAG, "forwardFileTransferInvitationToUi failed: cannot parse contact");
        }
        return;
    }
    Intent intent = ReceiveFileTransfer.forgeInvitationIntent(this, ftDao, invitation);
    /*
     * If the PendingIntent has the same operation, action, data, categories, components, and
     * flags it will be replaced. Invitation should be notified individually so we use a random
     * generator to provide a unique request code and reuse it for the notification.
     */
    int uniqueId = Utils.getUniqueIdForPendingIntent();
    PendingIntent pi = PendingIntent.getActivity(this, uniqueId, intent, PendingIntent.FLAG_ONE_SHOT);

    String displayName = RcsContactUtil.getInstance(this).getDisplayName(contact);
    String title = getString(R.string.title_recv_file_transfer);
    String message = getString(R.string.label_from_args, displayName);

    /* Send notification */
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    Notification notif = buildNotification(pi, title, message);
    notificationManager.notify(uniqueId, notif);
    TalkList.notifyNewConversationEvent(this, FileTransferIntent.ACTION_NEW_INVITATION);
}

From source file:com.deltadna.android.sdk.notifications.NotificationListenerService.java

/**
 * Creates a {@link android.support.v4.app.NotificationCompat.Builder}
 * whose built result will be posted on the {@link NotificationManager}.
 *
 * Implementations which call//  www . ja v  a  2 s .  c  o m
 * {@link NotificationCompat.Builder#setContentIntent(PendingIntent)}
 * or
 * {@link NotificationCompat.Builder#setDeleteIntent(PendingIntent)}
 * on the {@link NotificationCompat.Builder} and thus override the default
 * behaviour should notify the SDK that the push notification has been
 * opened or dismissed respectively.
 *
 * @param data  the data from the message
 *
 * @return      configured notification builder
 *
 * @see NotificationInteractionReceiver
 */
protected NotificationCompat.Builder createNotification(Bundle data) {
    final String title = getTitle(data);
    final String alert;
    if (data.containsKey(PLATFORM_ALERT)) {
        alert = data.getString(PLATFORM_ALERT);
    } else {
        Log.w(TAG, "Missing 'alert' key in message");
        alert = "Missing 'alert' key";
    }

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(getIcon())
            .setContentTitle(title).setContentText(alert).setAutoCancel(true);

    // to make the behaviour consistent with iOS on Unity
    final boolean backgrounded = !Utils.inForeground(this);
    if (!backgrounded) {
        Log.d(TAG, "Notifying SDK of notification opening");
        DDNANotifications.recordNotificationOpened(data, false);
    }

    builder.setContentIntent(
            PendingIntent.getBroadcast(this, 0,
                    new Intent(Actions.NOTIFICATION_OPENED).putExtra(DDNANotifications.EXTRA_PAYLOAD, data)
                            .putExtra(DDNANotifications.EXTRA_LAUNCH, backgrounded),
                    PendingIntent.FLAG_ONE_SHOT));
    builder.setDeleteIntent(PendingIntent.getBroadcast(this, 0,
            new Intent(Actions.NOTIFICATION_DISMISSED).putExtra(DDNANotifications.EXTRA_PAYLOAD, data),
            PendingIntent.FLAG_ONE_SHOT));

    return builder;
}

From source file:com.gsma.rcs.ri.extension.MultiMediaSessionIntentService.java

private void addSessionInvitationNotification(Intent intent, ContactId contact) {
    /* Create pending intent */
    Intent invitation = new Intent(intent);
    String title;/*  w  w w. j ava 2s. co  m*/
    if (mMultimediaMessagingSession) {
        invitation.setClass(this, MessagingSessionView.class);
        title = getString(R.string.title_recv_messaging_session);
    } else {
        invitation.setClass(this, StreamingSessionView.class);
        title = getString(R.string.title_recv_streaming_session);
    }
    invitation.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    /*
     * If the PendingIntent has the same operation, action, data, categories, components, and
     * flags it will be replaced. Invitation should be notified individually so we use a random
     * generator to provide a unique request code and reuse it for the notification.
     */
    int uniqueId = Utils.getUniqueIdForPendingIntent();
    PendingIntent contentIntent = PendingIntent.getActivity(this, uniqueId, invitation,
            PendingIntent.FLAG_ONE_SHOT);

    String displayName = RcsContactUtil.getInstance(this).getDisplayName(contact);

    /* Create notification */
    NotificationCompat.Builder notif = new NotificationCompat.Builder(this);
    notif.setContentIntent(contentIntent);
    notif.setSmallIcon(R.drawable.ri_notif_mm_session_icon);
    notif.setWhen(System.currentTimeMillis());
    notif.setAutoCancel(true);
    notif.setOnlyAlertOnce(true);
    notif.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    notif.setDefaults(Notification.DEFAULT_VIBRATE);
    notif.setContentTitle(title);
    notif.setContentText(getString(R.string.label_from_args, displayName));

    /* Send notification */
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.notify(uniqueId, notif.build());
}

From source file:com.group13.androidsdk.mycards.NotificationService.java

private void rescheduleNotifications() {

    List<NotificationRule> rules = new ArrayList<>();
    Collections.addAll(rules, MyCardsDBManager.getInstance(this).getAllNotificationRules());
    rules.addAll(getCalendarAsNotificationRules());

    SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    long lastNotifMillis = prefs.getLong("lastNotifElapsedRealtime", -2 * FIFTEEN_MINUTES);
    SharedPreferences.Editor prefEditor = prefs.edit();

    MyCardsDBManager dbm = MyCardsDBManager.getInstance(this);
    if (!(Math.abs(lastNotifMillis - SystemClock.elapsedRealtime()) < FIFTEEN_MINUTES || dbm.getDoNotDisturb()
            || dateMatchesAnyRule(new Date(), rules)
            || dbm.getCardsForReviewBefore(new Date(), null).length == 0)) {
        lastNotifMillis = SystemClock.elapsedRealtime();
        prefEditor.putLong("lastNotifElapsedRealtime", lastNotifMillis);

        sendNotification();/*  w  ww . j  a  v  a2s .c o m*/
    }

    Intent intent = new Intent(this, NotificationService.class);
    PendingIntent pi = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + FIFTEEN_MINUTES, pi);

    prefEditor.putLong("lastRunElapsedRealtime", SystemClock.elapsedRealtime());
    prefEditor.apply();
}

From source file:de.tum.in.tumcampus.services.GcmIntentService.java

private void sendNotification(Bundle extras) {
    //Catch newer gcm messages which can be different from chat messages
    //Dismiss them for now
    if (extras.containsKey("payload") && extras.containsKey("type")) {
        return;//w ww .j  a va  2 s  . co  m
    }

    //Get the update details
    int chatRoomId = Integer.parseInt(extras.getString("room"));
    int memberId = Integer.parseInt(extras.getString("member"));
    int messageId = -1;
    if (extras.containsKey("message")) {
        messageId = Integer.parseInt(extras.getString("message"));
    }

    Utils.logv(
            "Received GCM notification: room=" + chatRoomId + " member=" + memberId + " message=" + messageId);

    // Get the data necessary for the ChatActivity
    ChatMember member = Utils.getSetting(this, Const.CHAT_MEMBER, ChatMember.class);
    ChatRoom chatRoom = ChatClient.getInstance(this).getChatRoom(chatRoomId);

    ChatMessageManager manager = new ChatMessageManager(this, chatRoom.getId());
    Cursor messages = manager.getNewMessages(this.getPrivateKeyFromSharedPrefs(), member, messageId);

    // Notify any open chat activity that a message has been received
    Intent intent = new Intent("chat-message-received");
    intent.putExtras(extras);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

    //Check if chat is currently open then don't show a notification if it is
    if (ChatActivity.mCurrentOpenChatRoom != null && chatRoomId == ChatActivity.mCurrentOpenChatRoom.getId()) {
        return;
    }

    String txt = null;
    if (messages.moveToFirst()) {
        do {
            if (txt == null)
                txt = messages.getString(3);
            else
                txt += "\n" + messages.getString(3);
        } while (messages.moveToNext());
    }

    // Put the data into the intent
    Intent notificationIntent = new Intent(this, ChatActivity.class);
    notificationIntent.putExtra(Const.CURRENT_CHAT_ROOM, new Gson().toJson(chatRoom));

    TaskStackBuilder sBuilder = TaskStackBuilder.create(this);
    sBuilder.addNextIntent(new Intent(this, MainActivity.class));
    sBuilder.addNextIntent(new Intent(this, ChatRoomsActivity.class));
    sBuilder.addNextIntent(notificationIntent);

    if (Utils.getSettingBool(this, "card_chat_phone", true) && messageId == -1) {

        PendingIntent contentIntent = sBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

        // Notification sound
        Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.message);

        String replyLabel = getResources().getString(R.string.reply_label);

        RemoteInput remoteInput = new RemoteInput.Builder(ChatActivity.EXTRA_VOICE_REPLY).setLabel(replyLabel)
                .build();

        // Create the reply action and add the remote input
        NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_reply,
                getString(R.string.reply_label), contentIntent).addRemoteInput(remoteInput).build();

        //Show a nice notification
        Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.tum_logo_notification).setContentTitle(chatRoom.getName().substring(4))
                .setStyle(new NotificationCompat.BigTextStyle().bigText(txt)).setContentText(txt)
                .setContentIntent(contentIntent).setDefaults(Notification.DEFAULT_VIBRATE)
                .setLights(0xff0000ff, 500, 500).setSound(sound).setAutoCancel(true)
                .extend(new NotificationCompat.WearableExtender().addAction(action)).build();

        NotificationManager mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(chatRoomId << 4 + NOTIFICATION_ID, notification);
    }
}