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);
}
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;
}
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;
}
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);
}
}
}
Aggregations