Search in sources :

Example 1 with LocalNotification

use of com.codename1.notifications.LocalNotification in project CodenameOne by codenameone.

the class AndroidImplementation method startBackgroundFetchService.

/**
 * Starts the background fetch service.
 */
public void startBackgroundFetchService() {
    LocalNotification n = new LocalNotification();
    n.setId(BACKGROUND_FETCH_NOTIFICATION_ID);
    cancelLocalNotification(BACKGROUND_FETCH_NOTIFICATION_ID);
    // We schedule a local notification
    // First callback will be at the repeat interval
    // We don't specify a repeat interval because the scheduleLocalNotification will
    // set that for us using the getPreferredBackgroundFetchInterval method.
    scheduleLocalNotification(n, System.currentTimeMillis() + getPreferredBackgroundFetchInterval() * 1000, 0);
}
Also used : LocalNotification(com.codename1.notifications.LocalNotification)

Example 2 with LocalNotification

use of com.codename1.notifications.LocalNotification in project CodenameOne by codenameone.

the class LocalNotificationPublisher method createAndroidNotification.

private Notification createAndroidNotification(Context context, LocalNotification localNotif, PendingIntent content) {
    Context ctx = context;
    int smallIcon = ctx.getResources().getIdentifier("ic_stat_notify", "drawable", ctx.getApplicationInfo().packageName);
    int icon = ctx.getResources().getIdentifier("icon", "drawable", ctx.getApplicationInfo().packageName);
    if (smallIcon == 0) {
        smallIcon = icon;
    } else {
        icon = smallIcon;
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
    builder.setContentTitle(localNotif.getAlertTitle());
    builder.setContentText(localNotif.getAlertBody());
    builder.setAutoCancel(true);
    if (localNotif.getBadgeNumber() > 0) {
        builder.setNumber(localNotif.getBadgeNumber());
    }
    String image = localNotif.getAlertImage();
    if (image != null && image.length() > 0) {
        if (image.startsWith("/")) {
            image = image.substring(1);
        }
        InputStream in;
        try {
            in = context.getAssets().open(image);
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap im = BitmapFactory.decodeStream(in, null, opts);
            builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(im));
        } catch (IOException ex) {
            Logger.getLogger(LocalNotificationPublisher.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    builder.setSmallIcon(smallIcon);
    builder.setContentIntent(content);
    String sound = localNotif.getAlertSound();
    if (sound != null && sound.length() > 0) {
        sound = sound.toLowerCase();
        builder.setSound(android.net.Uri.parse("android.resource://" + ctx.getApplicationInfo().packageName + "/raw" + sound.substring(0, sound.indexOf("."))));
    }
    Notification n = builder.build();
    n.icon = icon;
    if (sound == null || sound.length() == 0) {
        n.defaults |= Notification.DEFAULT_SOUND;
    }
    return n;
}
Also used : Context(android.content.Context) InputStream(java.io.InputStream) IOException(java.io.IOException) LocalNotification(com.codename1.notifications.LocalNotification) Notification(android.app.Notification) Bitmap(android.graphics.Bitmap) NotificationCompat(android.support.v4.app.NotificationCompat) BitmapFactory(android.graphics.BitmapFactory)

Example 3 with LocalNotification

use of com.codename1.notifications.LocalNotification in project CodenameOne by codenameone.

the class AndroidImplementation method createNotificationFromBundle.

static LocalNotification createNotificationFromBundle(Bundle b) {
    LocalNotification n = new LocalNotification();
    n.setId(b.getString("NOTIF_ID"));
    n.setAlertTitle(b.getString("NOTIF_TITLE"));
    n.setAlertBody(b.getString("NOTIF_BODY"));
    n.setAlertSound(b.getString("NOTIF_SOUND"));
    n.setAlertImage(b.getString("NOTIF_IMAGE"));
    n.setBadgeNumber(b.getInt("NOTIF_NUMBER"));
    return n;
}
Also used : LocalNotification(com.codename1.notifications.LocalNotification)

Example 4 with LocalNotification

use of com.codename1.notifications.LocalNotification in project CodenameOne by codenameone.

the class LocalNotificationPublisher method onReceive.

public void onReceive(Context context, Intent intent) {
    // Fire the notification to the display
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Bundle extras = intent.getExtras();
    PendingIntent content = extras.getParcelable(NOTIFICATION_INTENT);
    Bundle b = extras.getParcelable(NOTIFICATION);
    LocalNotification notif = AndroidImplementation.createNotificationFromBundle(b);
    if (AndroidImplementation.BACKGROUND_FETCH_NOTIFICATION_ID.equals(notif.getId())) {
        PendingIntent backgroundFetchIntent = extras.getParcelable(BACKGROUND_FETCH_INTENT);
        if (backgroundFetchIntent != null) {
            try {
                backgroundFetchIntent.send();
            } catch (Exception ex) {
                Log.e("Codename One", "Failed to send BackgroundFetchHandler intent", ex);
            }
        } else {
            Log.d("Codename One", "BackgroundFetch intent was null");
        }
    } else {
        Notification notification = createAndroidNotification(context, notif, content);
        notification.when = System.currentTimeMillis();
        try {
            int notifId = Integer.parseInt(notif.getId());
            notificationManager.notify("CN1", notifId, notification);
        } catch (Exception e) {
            // that was a mistake, the first param is the tag not the id
            notificationManager.notify(notif.getId(), 0, notification);
        }
    }
}
Also used : NotificationManager(android.app.NotificationManager) Bundle(android.os.Bundle) PendingIntent(android.app.PendingIntent) LocalNotification(com.codename1.notifications.LocalNotification) IOException(java.io.IOException) LocalNotification(com.codename1.notifications.LocalNotification) Notification(android.app.Notification)

Aggregations

LocalNotification (com.codename1.notifications.LocalNotification)4 Notification (android.app.Notification)2 IOException (java.io.IOException)2 NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Context (android.content.Context)1 Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)1 Bundle (android.os.Bundle)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 InputStream (java.io.InputStream)1