use of com.codename1.push.PushActionsProvider in project CodenameOne by codenameone.
the class IOSImplementation method initPushActionCategories.
public static void initPushActionCategories() {
if (pushCallback instanceof PushActionsProvider) {
PushActionsProvider actionsProvider = (PushActionsProvider) pushCallback;
PushActionCategory[] categories = actionsProvider.getPushActionCategories();
if (categories != null) {
PushAction[] actions = PushActionCategory.getAllActions(categories);
for (PushAction action : actions) {
nativeInstance.registerPushAction(action.getId(), action.getTitle(), action.getTextInputPlaceholder(), action.getTextInputButtonText());
}
for (PushActionCategory category : categories) {
nativeInstance.startPushActionCategory(category.getId());
for (PushAction action : category.getActions()) {
nativeInstance.addPushActionToCategory(action.getId());
}
nativeInstance.endPushActionCategory();
}
nativeInstance.registerPushCategories();
}
}
}
use of com.codename1.push.PushActionsProvider in project CodenameOne by codenameone.
the class AndroidImplementation method installNotificationActionCategories.
/**
* Action categories are defined on the Main class by implementing the PushActionsProvider, however
* the main class may not be available to the push receiver, so we need to save these categories
* to the file system when the app is installed, then the push receiver can load these actions
* when it sends a push while the app isn't running.
* @param provider A reference to the App's main class
* @throws IOException
*/
public static void installNotificationActionCategories(PushActionsProvider provider) throws IOException {
// Assume that CN1 is running... this will run when the app starts
// up
Context context = getContext();
boolean requiresUpdate = false;
File categoriesFile = new File(activity.getFilesDir().getAbsolutePath() + "/" + FILE_NAME_NOTIFICATION_CATEGORIES);
if (!categoriesFile.exists()) {
requiresUpdate = true;
}
if (!requiresUpdate) {
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getApplicationContext().getPackageName(), PackageManager.GET_PERMISSIONS);
if (packageInfo.lastUpdateTime > categoriesFile.lastModified()) {
requiresUpdate = true;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (!requiresUpdate) {
return;
}
OutputStream os = getContext().openFileOutput(FILE_NAME_NOTIFICATION_CATEGORIES, 0);
PushActionCategory[] categories = provider.getPushActionCategories();
javax.xml.parsers.DocumentBuilderFactory docFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder docBuilder;
try {
docBuilder = docFactory.newDocumentBuilder();
} catch (ParserConfigurationException ex) {
Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
throw new IOException("Faield to create document builder for creating notification categories XML document", ex);
}
// root elements
org.w3c.dom.Document doc = docBuilder.newDocument();
org.w3c.dom.Element root = (org.w3c.dom.Element) doc.createElement("categories");
doc.appendChild(root);
for (PushActionCategory category : categories) {
org.w3c.dom.Element categoryEl = (org.w3c.dom.Element) doc.createElement("category");
org.w3c.dom.Attr idAttr = doc.createAttribute("id");
idAttr.setValue(category.getId());
categoryEl.setAttributeNode(idAttr);
for (PushAction action : category.getActions()) {
org.w3c.dom.Element actionEl = (org.w3c.dom.Element) doc.createElement("action");
org.w3c.dom.Attr actionIdAttr = doc.createAttribute("id");
actionIdAttr.setValue(action.getId());
actionEl.setAttributeNode(actionIdAttr);
org.w3c.dom.Attr actionTitleAttr = doc.createAttribute("title");
if (action.getTitle() != null) {
actionTitleAttr.setValue(action.getTitle());
} else {
actionTitleAttr.setValue(action.getId());
}
actionEl.setAttributeNode(actionTitleAttr);
if (action.getIcon() != null) {
org.w3c.dom.Attr actionIconAttr = doc.createAttribute("icon");
String iconVal = action.getIcon();
try {
// We'll store the resource IDs for the icon
// rather than the icon name because that is what
// the push notifications require.
iconVal = "" + context.getResources().getIdentifier(iconVal, "drawable", context.getPackageName());
actionIconAttr.setValue(iconVal);
actionEl.setAttributeNode(actionIconAttr);
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (action.getTextInputPlaceholder() != null) {
org.w3c.dom.Attr textInputPlaceholderAttr = doc.createAttribute("textInputPlaceholder");
textInputPlaceholderAttr.setValue(action.getTextInputPlaceholder());
actionEl.setAttributeNode(textInputPlaceholderAttr);
}
if (action.getTextInputButtonText() != null) {
org.w3c.dom.Attr textInputButtonTextAttr = doc.createAttribute("textInputButtonText");
textInputButtonTextAttr.setValue(action.getTextInputButtonText());
actionEl.setAttributeNode(textInputButtonTextAttr);
}
categoryEl.appendChild(actionEl);
}
root.appendChild(categoryEl);
}
try {
javax.xml.transform.TransformerFactory transformerFactory = javax.xml.transform.TransformerFactory.newInstance();
javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
javax.xml.transform.dom.DOMSource source = new javax.xml.transform.dom.DOMSource(doc);
javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(os);
transformer.transform(source, result);
} catch (Exception ex) {
throw new IOException("Failed to save notification categories as XML.", ex);
}
}
use of com.codename1.push.PushActionsProvider in project CodenameOne by codenameone.
the class AndroidImplementation method addActionsToNotification.
/**
* Adds actions to a push notification. This is called by the Push broadcast receiver probably before
* Codename One is initialized
* @param provider Reference to the app's main class which implements PushActionsProvider
* @param categoryId The category ID of the push notification.
* @param builder The builder for the push notification.
* @param targetIntent The target intent... this should go to the app's main Activity.
* @param context The current context (inside the Broadcast receiver).
* @throws IOException
*/
public static void addActionsToNotification(PushActionsProvider provider, String categoryId, NotificationCompat.Builder builder, Intent targetIntent, Context context) throws IOException {
// NOTE: THis will likely run when the main activity isn't running so we won't have
// access to any display properties... just native Android APIs will be accessible.
PushActionCategory category = null;
PushActionCategory[] categories;
if (provider != null) {
categories = provider.getPushActionCategories();
} else {
categories = getInstalledPushActionCategories(context);
}
for (PushActionCategory candidateCategory : categories) {
if (categoryId.equals(candidateCategory.getId())) {
category = candidateCategory;
break;
}
}
if (category == null) {
return;
}
int requestCode = 1;
for (PushAction action : category.getActions()) {
Intent newIntent = (Intent) targetIntent.clone();
newIntent.putExtra("pushActionId", action.getId());
PendingIntent contentIntent = createPendingIntent(context, requestCode++, newIntent);
try {
int iconId = 0;
try {
iconId = Integer.parseInt(action.getIcon());
} catch (Exception ex) {
}
// android.app.Notification.Action.Builder actionBuilder = new android.app.Notification.Action.Builder(iconId, action.getTitle(), contentIntent);
System.out.println("Adding action " + action.getId() + ", " + action.getTitle() + ", icon=" + iconId);
if (ActionWrapper.BuilderWrapper.isSupported()) {
// We need to take this abstracted "wrapper" approach because the Action.Builder class, and RemoteInput class
// aren't available until API 22.
// These classes use reflection to provide support for these classes safely.
ActionWrapper.BuilderWrapper actionBuilder = new ActionWrapper.BuilderWrapper(iconId, action.getTitle(), contentIntent);
if (action.getTextInputPlaceholder() != null && RemoteInputWrapper.isSupported()) {
RemoteInputWrapper.BuilderWrapper remoteInputBuilder = new RemoteInputWrapper.BuilderWrapper(action.getId() + "$Result");
remoteInputBuilder.setLabel(action.getTextInputPlaceholder());
RemoteInputWrapper remoteInput = remoteInputBuilder.build();
actionBuilder.addRemoteInput(remoteInput);
}
ActionWrapper actionWrapper = actionBuilder.build();
new NotificationCompatWrapper.BuilderWrapper(builder).addAction(actionWrapper);
} else {
builder.addAction(iconId, action.getTitle(), contentIntent);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Aggregations