Skip to content
This repository has been archived by the owner on Nov 24, 2019. It is now read-only.

Using NotificationCompat.builder for creating notifications #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added dist/bencoding.alarmmanager-android-0.12.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ btn1.addEventListener('click',function(e){
icon: Ti.App.Android.R.drawable.appicon, //Optional icon must be a resource id or url
minute:2, //Set the number of minutes until the alarm should go off
contentTitle:'Alarm #1', //Set the title of the Notification that will appear
contentText:'Alarm & Notify Basic', //Set the body of the notification that will apear
contentText:'Alarm & Notify Basic with some long text that spans over multiple lines', //Set the body of the notification that will apear
playSound:true, //On notification play the default sound ( by default off )
vibrate:true, //On notification vibrate device ( by default off )
showLights: true, //On notification show the device lights ( by default off )
Expand Down
2 changes: 1 addition & 1 deletion manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 0.11
version: 0.12
apiversion: 2
description: This module provides Titanium access to Androids Alarm Manager functionality
author: Benjamin Bahrenburg
Expand Down
65 changes: 39 additions & 26 deletions src/bencoding/alarmmanager/AlarmNotificationListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

import android.R;
import android.app.Notification;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.app.NotificationCompat.BigTextStyle;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.net.Uri;
Expand Down Expand Up @@ -61,47 +64,57 @@ public void onReceive(Context context, Intent intent) {

Intent notifyIntent =createIntent(className);

Notification notification = new Notification(icon, contentTitle, System.currentTimeMillis());
PendingIntent sender = PendingIntent.getActivity( TiApplication.getInstance().getApplicationContext(),
requestCode, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);


utils.debugLog("Using NotificationCompat.Builder");
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
TiApplication.getInstance().getApplicationContext())
.setWhen(System.currentTimeMillis())
.setContentText(contentText)
.setContentTitle(contentTitle)
.setSmallIcon(icon)
.setAutoCancel(true)
.setTicker(contentTitle)
.setContentIntent(sender)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(contentText)).setOnlyAlertOnce(true)
.setAutoCancel(true);

utils.debugLog("setting notification flags");
notification = createNotifyFlags(notification,playSound,hasCustomSound,soundPath,doVibrate,showLights);
utils.debugLog("setLatestEventInfo");
notification.setLatestEventInfo(TiApplication.getInstance().getApplicationContext(), contentTitle,contentText, sender);
notificationBuilder = createNotifyFlags(notificationBuilder,playSound,hasCustomSound,soundPath,doVibrate,showLights);
utils.debugLog("Notifying using requestCode =" + requestCode);
notificationManager.notify(requestCode, notification);
notificationManager.notify(requestCode, notificationBuilder.build());
utils.infoLog("You should now see a notification");

}

private Notification createNotifyFlags(Notification notification, boolean playSound, boolean hasCustomSound, String soundPath, boolean doVibrate, boolean showLights){
private NotificationCompat.Builder createNotifyFlags(NotificationCompat.Builder notification, boolean playSound, boolean hasCustomSound, String soundPath, boolean doVibrate, boolean showLights){
//Set the notifications flags
if(playSound){
if(hasCustomSound){
notification.sound = Uri.parse(soundPath);
}else{
notification.defaults |= Notification.DEFAULT_SOUND;
}
}

if(doVibrate){
notification.defaults |=Notification.DEFAULT_VIBRATE;
}
if(showLights){
notification.defaults |=Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
}
if (playSound && !hasCustomSound && doVibrate && showLights){
notification.defaults = Notification.DEFAULT_ALL;
notification.setDefaults(Notification.DEFAULT_ALL);
}else{
int defaults = 0;
if (showLights) {
defaults = defaults | Notification.DEFAULT_LIGHTS;
notification.setLights(0xFF0000FF,200,3000);
}
if (playSound) {
defaults = defaults | Notification.DEFAULT_SOUND;
if(hasCustomSound){
notification.setSound(Uri.parse(soundPath));
}
}
if (doVibrate) {
defaults = defaults | Notification.DEFAULT_VIBRATE;
}
notification.setDefaults(defaults);
}

//Set alarm flags
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
return notification;
}

private Intent createIntent(String className){
try {

Expand Down