Search in sources :

Example 1 with AndroidPushTemplate

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

the class BackendlessPushService method registerFurther.

private void registerFurther(final Context context, String GCMregistrationId) {
    final long registrationExpiration = GCMRegistrar.getRegistrationExpiration(context);
    Backendless.Messaging.registerDeviceOnServer(GCMregistrationId, new ArrayList<>(GCMRegistrar.getChannels(context)), registrationExpiration, new AsyncCallback<String>() {

        @Override
        public void handleResponse(String registrationInfo) {
            String ids;
            try {
                Object[] obj = (Object[]) weborb.util.io.Serializer.fromBytes(registrationInfo.getBytes(), weborb.util.io.Serializer.JSON, false);
                ids = (String) obj[0];
                PushTemplateHelper.deleteNotificationChannel(context);
                Map<String, AndroidPushTemplate> templates = (Map<String, AndroidPushTemplate>) obj[1];
                if (android.os.Build.VERSION.SDK_INT > 25)
                    for (AndroidPushTemplate templ : templates.values()) PushTemplateHelper.getOrCreateNotificationChannel(context.getApplicationContext(), templ);
                PushTemplateHelper.setPushNotificationTemplates(templates, registrationInfo.getBytes());
            } catch (IOException e) {
                callback.onError(context, "Could not deserialize server response: " + e.getMessage());
                return;
            }
            GCMRegistrar.setRegistrationIds(context, ids, registrationExpiration);
            callback.onRegistered(context, registrationInfo);
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            callback.onError(context, "Could not register device on Backendless server: " + fault.toString());
        }
    });
}
Also used : AndroidPushTemplate(com.backendless.messaging.AndroidPushTemplate) IOException(java.io.IOException) Map(java.util.Map) BackendlessFault(com.backendless.exceptions.BackendlessFault)

Example 2 with AndroidPushTemplate

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

the class BackendlessPushService method handleMessage.

private void handleMessage(final Context context, Intent intent) {
    final int messageId = intent.getIntExtra(BackendlessBroadcastReceiver.EXTRA_MESSAGE_ID, 0);
    final String message = intent.getStringExtra(PublishOptions.MESSAGE_TAG);
    try {
        final String templateName = intent.getStringExtra(PublishOptions.TEMPLATE_NAME);
        if (templateName != null) {
            if (PushTemplateHelper.getPushNotificationTemplates() == null)
                PushTemplateHelper.restorePushTemplates();
            AndroidPushTemplate androidPushTemplate = PushTemplateHelper.getPushNotificationTemplates().get(templateName);
            if (androidPushTemplate != null) {
                if (androidPushTemplate.getContentAvailable() != null && androidPushTemplate.getContentAvailable() == 1) {
                    callback.onMessage(context, intent);
                    return;
                }
                Notification notification = PushTemplateHelper.convertFromTemplate(context, androidPushTemplate, message, messageId);
                PushTemplateHelper.showNotification(context, notification, androidPushTemplate.getName(), messageId);
            }
            return;
        }
        String immediatePush = intent.getStringExtra(PublishOptions.ANDROID_IMMEDIATE_PUSH);
        if (immediatePush != null) {
            AndroidPushTemplate androidPushTemplate = (AndroidPushTemplate) weborb.util.io.Serializer.fromBytes(immediatePush.getBytes(), weborb.util.io.Serializer.JSON, false);
            if (androidPushTemplate.getContentAvailable() != null && androidPushTemplate.getContentAvailable() == 1) {
                callback.onMessage(context, intent);
                return;
            }
            androidPushTemplate.setName("ImmediateMessage");
            Notification notification = PushTemplateHelper.convertFromTemplate(context, androidPushTemplate, message, messageId);
            PushTemplateHelper.showNotification(context, notification, androidPushTemplate.getName(), messageId);
            return;
        }
        boolean showPushNotification = callback.onMessage(context, intent);
        if (showPushNotification) {
            CharSequence tickerText = intent.getStringExtra(PublishOptions.ANDROID_TICKER_TEXT_TAG);
            CharSequence contentTitle = intent.getStringExtra(PublishOptions.ANDROID_CONTENT_TITLE_TAG);
            if (tickerText != null && tickerText.length() > 0) {
                final String contentText = intent.getStringExtra(PublishOptions.ANDROID_CONTENT_TEXT_TAG);
                int appIcon = context.getApplicationInfo().icon;
                if (appIcon == 0)
                    appIcon = android.R.drawable.sym_def_app_icon;
                Intent notificationIntent = context.getPackageManager().getLaunchIntentForPackage(context.getApplicationInfo().packageName);
                PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
                Notification notification = new Notification.Builder(context).setSmallIcon(appIcon).setTicker(tickerText).setContentTitle(contentTitle).setContentText(contentText).setContentIntent(contentIntent).setWhen(System.currentTimeMillis()).build();
                notification.flags |= Notification.FLAG_AUTO_CANCEL;
                int customLayout = context.getResources().getIdentifier("notification", "layout", context.getPackageName());
                int customLayoutTitle = context.getResources().getIdentifier("title", "id", context.getPackageName());
                int customLayoutDescription = context.getResources().getIdentifier("text", "id", context.getPackageName());
                int customLayoutImageContainer = context.getResources().getIdentifier("image", "id", context.getPackageName());
                int customLayoutImage = context.getResources().getIdentifier("push_icon", "drawable", context.getPackageName());
                if (customLayout > 0 && customLayoutTitle > 0 && customLayoutDescription > 0 && customLayoutImageContainer > 0) {
                    NotificationLookAndFeel lookAndFeel = new NotificationLookAndFeel();
                    lookAndFeel.extractColors(context);
                    RemoteViews contentView = new RemoteViews(context.getPackageName(), customLayout);
                    contentView.setTextViewText(customLayoutTitle, contentTitle);
                    contentView.setTextViewText(customLayoutDescription, contentText);
                    contentView.setTextColor(customLayoutTitle, lookAndFeel.getTextColor());
                    contentView.setFloat(customLayoutTitle, "setTextSize", lookAndFeel.getTextSize());
                    contentView.setTextColor(customLayoutDescription, lookAndFeel.getTextColor());
                    contentView.setFloat(customLayoutDescription, "setTextSize", lookAndFeel.getTextSize());
                    contentView.setImageViewResource(customLayoutImageContainer, customLayoutImage);
                    notification.contentView = contentView;
                }
                NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.notify(intent.getIntExtra(BackendlessBroadcastReceiver.EXTRA_MESSAGE_ID, 0), notification);
            }
        }
    } catch (Throwable throwable) {
        Log.e(TAG, "Error processing push notification", throwable);
    }
}
Also used : NotificationManager(android.app.NotificationManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification) RemoteViews(android.widget.RemoteViews) AndroidPushTemplate(com.backendless.messaging.AndroidPushTemplate) PendingIntent(android.app.PendingIntent)

Aggregations

AndroidPushTemplate (com.backendless.messaging.AndroidPushTemplate)2 Notification (android.app.Notification)1 NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 RemoteViews (android.widget.RemoteViews)1 BackendlessFault (com.backendless.exceptions.BackendlessFault)1 IOException (java.io.IOException)1 Map (java.util.Map)1