I'm trying to make an demo which can show a custom notification periodically.
To do that, I used Remoteviews to update the view of a notification. Put this notification into onReceive method of an BroadcastReceiver. Then call it periodically by using AlarmManager.
Everything works just fine unless my demo application is removed from recent applications. It's force crashed and when I checked in the android log, I saw this error : "Bad notification posted - Couldn't expand RemoteViews for: StatusBarNotification"
Could you please help me solve this ?
Below is my code :
Intent alarmIntent = new Intent(this, PeriodicTaskReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
PeriodicTaskReceiver is my customized BroadcastReceiver class:
public void onReceive(Context context, Intent intent) {
repository = new LocalRepository();
Notification(context, repository.getData());
}
}
Notification method :
public void Notification(Context context, Data data) {
// Set Notification Title
String strtitle = context.getString(R.string.notificationtitle);
// Open NotificationView Class on Notification Click
Intent intent = new Intent(context, NotificationView.class);
// Send data to NotificationView Class
intent.putExtra("title", strtitle);
// Open NotificationView.java Activity
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews customNotification = buildNotificationItem(data);
// Create Notification using NotificationCompat.Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context)
// Set Icon
.setSmallIcon(R.drawable.icon)
// Set PendingIntent into Notification
.setContentIntent(pIntent)
.setContent(customNotification);
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = builder.build();
// notification.bigContentView = customNotification;
// Build Notification with Notification Manager
notificationmanager.notify(0, notification);
}
And the method handle the content for my Remoteviews :
private RemoteViews buildNotificationItem(Data data){
RemoteViews view = new RemoteViews(Global.PACKAGE_NAME, R.layout.notification_item);
view.setTextViewText(R.id.notification_tags, data.getTitle());
view.setTextViewText(R.id.notification_content, data.getContent());
return view;
}
Thanks