Search in sources :

Example 1 with Action

use of com.backendless.messaging.Action in project Android-SDK by Backendless.

the class PushTemplateHelper method convertFromTemplate.

static Notification convertFromTemplate(Context context, AndroidPushTemplate template, String messageText, int messageId) {
    context = context.getApplicationContext();
    // Notification channel ID is ignored for Android 7.1.1 (API level 25) and lower.
    NotificationCompat.Builder notificationBuilder;
    // android.os.Build.VERSION_CODES.O == 26
    if (android.os.Build.VERSION.SDK_INT > 25) {
        NotificationChannel notificationChannel = getOrCreateNotificationChannel(context, template);
        notificationBuilder = new NotificationCompat.Builder(context, notificationChannel.getId());
        if (template.getColorized() != null)
            notificationBuilder.setColorized(template.getColorized());
        if (template.getBadge() != null && (template.getBadge() == NotificationCompat.BADGE_ICON_SMALL || template.getBadge() == NotificationCompat.BADGE_ICON_LARGE))
            notificationBuilder.setBadgeIconType(template.getBadge());
        else
            notificationBuilder.setBadgeIconType(NotificationCompat.BADGE_ICON_NONE);
        if (template.getCancelAfter() != null && template.getCancelAfter() != 0)
            notificationBuilder.setTimeoutAfter(template.getCancelAfter() * 1000);
    } else {
        notificationBuilder = new NotificationCompat.Builder(context);
        if (template.getPriority() != null && template.getPriority() > 0 && template.getPriority() < 6)
            notificationBuilder.setPriority(template.getPriority() - 3);
        else
            notificationBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        if (notificationBuilder.getPriority() > NotificationCompat.PRIORITY_LOW) {
            Uri soundUri;
            if (template.getButtonTemplate() != null && template.getButtonTemplate().getSound() != null && !template.getButtonTemplate().getSound().isEmpty()) {
                int soundResource = context.getResources().getIdentifier(template.getButtonTemplate().getSound(), "raw", context.getPackageName());
                soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + soundResource);
            } else
                soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            notificationBuilder.setSound(soundUri, AudioManager.STREAM_NOTIFICATION);
        }
        if (template.getButtonTemplate().getVibrate() != null && template.getButtonTemplate().getVibrate().length > 0 && notificationBuilder.getPriority() > NotificationCompat.PRIORITY_LOW) {
            long[] vibrate = new long[template.getButtonTemplate().getVibrate().length];
            int index = 0;
            for (long l : template.getButtonTemplate().getVibrate()) vibrate[index++] = l;
            notificationBuilder.setVibrate(vibrate);
        }
        if (template.getButtonTemplate().getVisibility() != null)
            notificationBuilder.setVisibility(template.getButtonTemplate().getVisibility());
        else
            notificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    }
    if (template.getAttachmentUrl() != null) {
        try {
            InputStream is = (InputStream) new URL(template.getAttachmentUrl()).getContent();
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            if (bitmap != null)
                notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap));
            else
                Log.i(PushTemplateHelper.class.getSimpleName(), "Cannot convert rich media for notification into bitmap.");
        } catch (IOException e) {
            Log.e(PushTemplateHelper.class.getSimpleName(), "Cannot receive rich media for notification.");
        }
    } else if (messageText.length() > 35) {
        NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle().setSummaryText(template.getThirdRowTitle()).setBigContentTitle(template.getFirstRowTitle()).bigText(messageText);
        notificationBuilder.setStyle(bigText);
    }
    if (template.getLargeIcon() != null) {
        if (template.getLargeIcon().startsWith("http")) {
            try {
                InputStream is = (InputStream) new URL(template.getLargeIcon()).getContent();
                Bitmap bitmap = BitmapFactory.decodeStream(is);
                if (bitmap != null)
                    notificationBuilder.setLargeIcon(bitmap);
                else
                    Log.i(PushTemplateHelper.class.getSimpleName(), "Cannot convert Large Icon into bitmap.");
            } catch (IOException e) {
                Log.e(PushTemplateHelper.class.getSimpleName(), "Cannot receive bitmap for Large Icon.");
            }
        } else {
            int largeIconResource = context.getResources().getIdentifier(template.getLargeIcon(), "raw", context.getPackageName());
            if (largeIconResource != 0) {
                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), largeIconResource);
                notificationBuilder.setLargeIcon(bitmap);
            }
        }
    }
    int icon = 0;
    if (template.getIcon() != null)
        icon = context.getResources().getIdentifier(template.getIcon(), "drawable", context.getPackageName());
    if (icon == 0)
        icon = context.getResources().getIdentifier("ic_launcher", "drawable", context.getPackageName());
    if (icon != 0)
        notificationBuilder.setSmallIcon(icon);
    if (template.getLightsColor() != null && template.getLightsOnMs() != null && template.getLightsOffMs() != null)
        notificationBuilder.setLights(template.getLightsColor() | 0xFF000000, template.getLightsOnMs(), template.getLightsOffMs());
    if (template.getColorCode() != null)
        notificationBuilder.setColor(template.getColorCode() | 0xFF000000);
    if (template.getCancelOnTap() != null)
        notificationBuilder.setAutoCancel(template.getCancelOnTap());
    else
        notificationBuilder.setAutoCancel(false);
    notificationBuilder.setShowWhen(true).setWhen(System.currentTimeMillis()).setContentTitle(template.getFirstRowTitle()).setSubText(template.getThirdRowTitle()).setTicker(template.getTickerText()).setContentText(messageText);
    Intent notificationIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
    notificationIntent.putExtra(BackendlessBroadcastReceiver.EXTRA_MESSAGE_ID, messageId);
    notificationIntent.putExtra(PublishOptions.TEMPLATE_NAME, template.getName());
    notificationIntent.putExtra(PublishOptions.MESSAGE_TAG, messageText);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(context, messageId * 3, notificationIntent, 0);
    notificationBuilder.setContentIntent(contentIntent);
    if (template.getButtonTemplate().getActions() != null) {
        List<NotificationCompat.Action> actions = createActions(context, template.getButtonTemplate().getActions(), template.getName(), messageId, messageText);
        for (NotificationCompat.Action action : actions) notificationBuilder.addAction(action);
    }
    return notificationBuilder.build();
}
Also used : Action(com.backendless.messaging.Action) InputStream(java.io.InputStream) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IOException(java.io.IOException) Uri(android.net.Uri) URL(java.net.URL) NotificationChannel(android.app.NotificationChannel) Bitmap(android.graphics.Bitmap) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Example 2 with Action

use of com.backendless.messaging.Action in project Android-SDK by Backendless.

the class PushTemplateHelper method createActions.

private static List<NotificationCompat.Action> createActions(Context context, Action[] actions, String templateName, int messageId, String messageText) {
    List<NotificationCompat.Action> notifActions = new ArrayList<>();
    int i = 1;
    for (Action a : actions) {
        Intent actionIntent = new Intent(a.getTitle());
        actionIntent.setClassName(context, a.getId());
        actionIntent.putExtra(BackendlessBroadcastReceiver.EXTRA_MESSAGE_ID, messageId);
        actionIntent.putExtra(PublishOptions.MESSAGE_TAG, messageText);
        actionIntent.putExtra(PublishOptions.TEMPLATE_NAME, templateName);
        actionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // user should use messageId and tag(templateName) to cancel notification.
        PendingIntent pendingIntent = PendingIntent.getActivity(context, messageId * 3 + i++, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action.Builder actionBuilder = new NotificationCompat.Action.Builder(0, a.getTitle(), pendingIntent);
        if (a.getOptions() == 1) {
            RemoteInput remoteInput = new RemoteInput.Builder(PublishOptions.INLINE_REPLY).build();
            actionBuilder.setAllowGeneratedReplies(true).addRemoteInput(remoteInput);
        }
        notifActions.add(actionBuilder.build());
    }
    return notifActions;
}
Also used : RemoteInput(android.support.v4.app.RemoteInput) Action(com.backendless.messaging.Action) ArrayList(java.util.ArrayList) NotificationCompat(android.support.v4.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

Example 3 with Action

use of com.backendless.messaging.Action in project Android-SDK by Backendless.

the class PushTemplateHelper method convertFromTemplate.

static Notification convertFromTemplate(final Context context, final AndroidPushTemplate template, final Bundle newBundle, int notificationId) {
    Context appContext = context.getApplicationContext();
    // Notification channel ID is ignored for Android 7.1.1 (API level 25) and lower.
    String messageText = newBundle.getString(PublishOptions.MESSAGE_TAG);
    String contentTitle = newBundle.getString(PublishOptions.ANDROID_CONTENT_TITLE_TAG);
    contentTitle = contentTitle != null ? contentTitle : template.getContentTitle();
    String summarySubText = newBundle.getString(PublishOptions.ANDROID_SUMMARY_SUBTEXT_TAG);
    summarySubText = summarySubText != null ? summarySubText : template.getSummarySubText();
    String largeIcon = newBundle.getString(PublishOptions.ANDROID_LARGE_ICON_TAG);
    largeIcon = largeIcon != null ? largeIcon : template.getLargeIcon();
    String attachmentUrl = newBundle.getString(PublishOptions.ANDROID_ATTACHMENT_URL_TAG);
    attachmentUrl = attachmentUrl != null ? attachmentUrl : template.getAttachmentUrl();
    NotificationCompat.Builder notificationBuilder;
    // android.os.Build.VERSION_CODES.O == 26
    if (android.os.Build.VERSION.SDK_INT > 25) {
        NotificationChannel notificationChannel = getOrCreateNotificationChannel(appContext, template);
        notificationBuilder = new NotificationCompat.Builder(appContext, notificationChannel.getId());
        if (template.getBadge() != null && (template.getBadge() == NotificationCompat.BADGE_ICON_SMALL || template.getBadge() == NotificationCompat.BADGE_ICON_LARGE))
            notificationBuilder.setBadgeIconType(template.getBadge());
        else
            notificationBuilder.setBadgeIconType(NotificationCompat.BADGE_ICON_NONE);
        if (template.getBadgeNumber() != null)
            notificationBuilder.setNumber(template.getBadgeNumber());
        if (template.getCancelAfter() != null && template.getCancelAfter() != 0)
            notificationBuilder.setTimeoutAfter(template.getCancelAfter() * 1000);
    } else {
        notificationBuilder = new NotificationCompat.Builder(appContext);
        if (template.getPriority() != null && template.getPriority() > 0 && template.getPriority() < 6)
            notificationBuilder.setPriority(template.getPriority() - 3);
        else
            notificationBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        if (notificationBuilder.getPriority() > NotificationCompat.PRIORITY_LOW)
            notificationBuilder.setSound(PushTemplateHelper.getSoundUri(appContext, template.getSound()), AudioManager.STREAM_NOTIFICATION);
        if (template.getVibrate() != null && template.getVibrate().length > 0 && notificationBuilder.getPriority() > NotificationCompat.PRIORITY_LOW) {
            long[] vibrate = new long[template.getVibrate().length];
            int index = 0;
            for (long l : template.getVibrate()) vibrate[index++] = l;
            notificationBuilder.setVibrate(vibrate);
        }
    }
    if (attachmentUrl != null) {
        try {
            InputStream is = (InputStream) new URL(attachmentUrl).getContent();
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            if (bitmap != null)
                notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap));
            else
                Log.i(PushTemplateHelper.class.getSimpleName(), "Cannot convert rich media for notification into bitmap.");
        } catch (IOException e) {
            Log.e(PushTemplateHelper.class.getSimpleName(), "Cannot receive rich media for notification.");
        }
    } else if (messageText.length() > 35) {
        NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle().setBigContentTitle(contentTitle).setSummaryText(summarySubText).bigText(messageText);
        notificationBuilder.setStyle(bigText);
    }
    if (largeIcon != null) {
        if (largeIcon.startsWith("http")) {
            try {
                InputStream is = (InputStream) new URL(largeIcon).getContent();
                Bitmap bitmap = BitmapFactory.decodeStream(is);
                if (bitmap != null)
                    notificationBuilder.setLargeIcon(bitmap);
                else
                    Log.i(PushTemplateHelper.class.getSimpleName(), "Cannot convert Large Icon into bitmap.");
            } catch (IOException e) {
                Log.e(PushTemplateHelper.class.getSimpleName(), "Cannot receive bitmap for Large Icon.");
            }
        } else {
            int largeIconResource = appContext.getResources().getIdentifier(largeIcon, "drawable", appContext.getPackageName());
            if (largeIconResource != 0) {
                Bitmap bitmap = BitmapFactory.decodeResource(appContext.getResources(), largeIconResource);
                notificationBuilder.setLargeIcon(bitmap);
            }
        }
    }
    int icon = 0;
    // try to get icon from template
    if (template.getIcon() != null) {
        icon = appContext.getResources().getIdentifier(template.getIcon(), "mipmap", appContext.getPackageName());
        if (icon == 0)
            icon = appContext.getResources().getIdentifier(template.getIcon(), "drawable", appContext.getPackageName());
    }
    // try to get default icon
    if (icon == 0) {
        icon = context.getApplicationInfo().icon;
        if (icon == 0)
            icon = android.R.drawable.sym_def_app_icon;
    }
    if (icon != 0)
        notificationBuilder.setSmallIcon(icon);
    if (template.getLightsColor() != null && template.getLightsOnMs() != null && template.getLightsOffMs() != null)
        notificationBuilder.setLights(template.getLightsColor() | 0xFF000000, template.getLightsOnMs(), template.getLightsOffMs());
    if (template.getColorCode() != null)
        notificationBuilder.setColor(template.getColorCode() | 0xFF000000);
    if (template.getCancelOnTap() != null)
        notificationBuilder.setAutoCancel(template.getCancelOnTap());
    else
        notificationBuilder.setAutoCancel(false);
    notificationBuilder.setShowWhen(true).setWhen(System.currentTimeMillis()).setContentTitle(contentTitle != null ? contentTitle : template.getContentTitle()).setSubText(summarySubText != null ? summarySubText : template.getSummarySubText()).setContentText(messageText);
    Intent notificationIntent;
    if (template.getActionOnTap() == null || template.getActionOnTap().isEmpty())
        notificationIntent = appContext.getPackageManager().getLaunchIntentForPackage(appContext.getPackageName());
    else {
        notificationIntent = new Intent("ActionOnTap");
        notificationIntent.setClassName(appContext, template.getActionOnTap());
    }
    notificationIntent.putExtras(newBundle);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(appContext, notificationId * 3, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    // user should use messageId and tag(templateName) to cancel notification.
    notificationBuilder.setContentIntent(contentIntent);
    if (template.getActions() != null) {
        List<NotificationCompat.Action> actions = createActions(appContext, template.getActions(), newBundle, notificationId);
        for (NotificationCompat.Action action : actions) notificationBuilder.addAction(action);
    }
    return notificationBuilder.build();
}
Also used : Context(android.content.Context) Action(com.backendless.messaging.Action) InputStream(java.io.InputStream) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IOException(java.io.IOException) URL(java.net.URL) NotificationChannel(android.app.NotificationChannel) Bitmap(android.graphics.Bitmap) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Example 4 with Action

use of com.backendless.messaging.Action in project Android-SDK by Backendless.

the class PushTemplateHelper method createActions.

private static List<NotificationCompat.Action> createActions(final Context appContext, final Action[] actions, final Bundle bundle, int notificationId) {
    List<NotificationCompat.Action> notifActions = new ArrayList<>();
    int i = 1;
    for (Action a : actions) {
        Intent actionIntent = new Intent(a.getTitle());
        actionIntent.setClassName(appContext, a.getId());
        actionIntent.putExtras(bundle);
        actionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // user should use messageId and tag(templateName) to cancel notification.
        PendingIntent pendingIntent = PendingIntent.getActivity(appContext, notificationId * 3 + i++, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action.Builder actionBuilder = new NotificationCompat.Action.Builder(0, a.getTitle(), pendingIntent);
        if (a.getOptions() == 1) {
            RemoteInput remoteInput = new RemoteInput.Builder(PublishOptions.INLINE_REPLY).build();
            actionBuilder.setAllowGeneratedReplies(true).addRemoteInput(remoteInput);
        }
        notifActions.add(actionBuilder.build());
    }
    return notifActions;
}
Also used : RemoteInput(android.support.v4.app.RemoteInput) Action(com.backendless.messaging.Action) ArrayList(java.util.ArrayList) NotificationCompat(android.support.v4.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

Aggregations

PendingIntent (android.app.PendingIntent)4 Intent (android.content.Intent)4 NotificationCompat (android.support.v4.app.NotificationCompat)4 Action (com.backendless.messaging.Action)4 NotificationChannel (android.app.NotificationChannel)2 Bitmap (android.graphics.Bitmap)2 RemoteInput (android.support.v4.app.RemoteInput)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 Context (android.content.Context)1 Uri (android.net.Uri)1