Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Update custom notifications - Android

$
0
0

Hi everybody,

I'm trying to work with local notification. The idea is display custom notifications and be able to update them managed by a library with two methods, one to create a notifications and another to update it.

index.js
var moment = require('alloy/moment'),
  localNotification = require('local_notifications');
 
function fireNotification() {  
  localNotification.localNotification('New info available. Check it now?');
};
 
function updateNotification(){
  localNotification.updateNotification(moment().format('HH:mm'));
};
 
setInterval (updateNotification, 15000);
 
var button1 = Ti.UI.createButton({title: 'Trigger Notification in background (in 4 secs)', top:200, width:Ti.UI.SIZE, height:Ti.UI.SIZE, color:'#FFF', backgroundColor:'#585858'});
button1.addEventListener('click', function(e){ 
  setTimeout(fireNotification, 4000);    
});
$.win.add(button1);
 
 
$.win.open();

local_notifications.js.js
/**
* Local Notification library
*
*     var localNotification=require('local_notifications');
*
*     //Throw a local notification. It only works Android
*     localNotification.localNotification('Text to display in notification');
*
* @class lib.local_notifications
*/
 
var notification, customView;
 
exports.localNotification = function(alertBody) {
 
  if (OS_ANDROID) {
 
    customView = Ti.Android.createRemoteViews({
        layoutId: Ti.App.Android.R.layout.customview
    });
 
    var intentTest = Ti.Android.createIntent({
        className : 'sh.dogfi.backgroundTasksApp.backgroundTasksAppActivity',
        packageName : Ti.App.id,
        action: Ti.Android.ACTION_MAIN
    });
    intentTest.flags = Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP;
    intentTest.addCategory(Ti.Android.CATEGORY_LAUNCHER);
 
    var downnloadIntent = Ti.Android.createPendingIntent({intent : intentTest});
    var cancelIntent = Ti.Android.createPendingIntent({intent : intentTest});
 
    // Reference elements in the layout by prefixing the IDs with 'Ti.App.Android.R.id'
    customView.setTextViewText(Ti.App.Android.R.id.message, alertBody);
    customView.setTextViewText(Ti.App.Android.R.id.okbutton, "See it!");
    customView.setOnClickPendingIntent(Ti.App.Android.R.id.okbutton, downnloadIntent);
    customView.setTextViewText(Ti.App.Android.R.id.cancelbutton, "Not now");
    customView.setOnClickPendingIntent(Ti.App.Android.R.id.cancelbutton, cancelIntent);
 
 
    notification = Titanium.Android.createNotification({
      contentView: customView,
      icon: Ti.App.Android.R.drawable.appicon
    });
    Ti.Android.NotificationManager.notify(1, notification);
 
  };
};
 
exports.updateNotification = function (param1){
   Ti.API.error('Checking param1 ' + param1);
  notification.contentView.setTextViewText(Ti.App.Android.R.id.message, param1);  //Option1: Trying to reach that text through the notification - It doesn't work
  customView.setTextViewText(Ti.App.Android.R.id.message, param1);                //Option2: Trying to reach the text through the customView object - It doesn't work either
 
};

customview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingLeft="16dp"
              android:paddingRight="16dp"
              android:background="#f00"
              android:orientation="horizontal" >
    <ImageView
            android:id="@+id/imageView3"
            android:layout_width="50dp"
            android:layout_height="150dp"
            android:layout_margin="5dp"
            android:background="#fff"
            android:paddingBottom="50dp"
            android:paddingLeft="3dp"
            android:paddingRight="3dp"
            android:paddingTop="3dp"
            android:scaleType="fitXY"
            android:src="@drawable/appicon" />
    <TextView android:id="@+id/message"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textSize="18dp"
              android:text="Default text" />
    <Button android:id="@+id/okbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OK" />
    <Button android:id="@+id/cancelbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel" />
</LinearLayout>

I'm being able to display the notification but I can't update the text, I've tried through

notification.contentView.setTextViewText(Ti.App.Android.R.id.message, param1);
and with
customView.setTextViewText(Ti.App.Android.R.id.message, param1);
but neither of those seem to give me access to the text of the customView. Console is clean of any error. I can see how the param1 brings the time each 15 secs, though no errors. Any ideas how I can update it?

Thanks a lot.

Application type: mobile Titanium SDK: 3.4.1 Platform & version: Android Device: Physical device Nexus 4 (Android 4.4.2) Titanium Studio: 3.4.1.201410281727 a simple test case: working code that demonstrates issue, formatted within the code markdown syntax provided Logs:


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>