I am working in a project where I need to use ViewFlipper view inside Remoteview in Notification bar. Currently am facing issue with a showNext() and showPreview(). But unfortunately showNext() and showPreview() are not called when I call the button. I am also posting my code for your reference. Kindly help me where I am making mistake and correct me if my question is not clear.
private Notification setCustomViewNotification() {
// Creates an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(this, ResultActivity.class);
// This ensures that the back button follows the recommended convention for the back key.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack.
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Create remote view and set bigContentView.
expandedView = new RemoteViews(this.getPackageName(), R.layout.notification_viewflipper);
// expandedView.set(R.id.text_view, "Neat logo!");
expandedView.setOnClickPendingIntent(R.id.img_left,getPendingSelfIntent(MainActivity.this, IMAGE_LEFT));
expandedView.setOnClickPendingIntent(R.id.img_right,getPendingSelfIntent(MainActivity.this, IMAGE_RIGHT));
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle("Custom View").build();
notification.bigContentView = expandedView;
return notification;
}
so when a click event is triggered I have to change the ViewFlipper items. Following is the code where I stuck up completely.
public void onEvent(ViewFlipperClickEvent event){
if(event.getTag().equals(MainActivity.IMAGE_LEFT)){
expandedView.showPrevious(R.id.ViewFlipper01);
}else if(event.getTag().equals(MainActivity.IMAGE_RIGHT)){
expandedView.showNext(R.id.ViewFlipper01);
}
}
Thanks in advance
You need to notify the change to your push notification
Try something like-
notificationBig.showNext(R.id.vf_carousel)
notificationManager.notify(0, customNotif.build())
Here 0 will be the unique ID of your notification that you need to update
Related
I wanted a notification which always stays in expanded mode and doesn't collapse when user tries to do it.
I tried setting :
setCustomContentView(null) and even not calling the former function.
I also tried all possible plays with other methods of NotificationBuilder but to no avail!
Can somebody suggest how to achieve the above? The reverse is quite easy to do but this would require some hack imo!
An expandable Notification is a special case of a Notification Big View. If the Big View is not at the top of the notification drawer, it it shown 'closed' and is expandable by swipe. Quote from Android Developers:
A notification's big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture. Expanded notifications are available starting with Android 4.1.
String someLongText="here is your long text",channelId="default";
Bitmap icon1 = BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
getApplicationContext()).setAutoCancel(true)
.setContentTitle("Exemplo 1")
.setChannelId (channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(icon1).setContentText("Hello World!");
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(someLongText);
bigText.setBigContentTitle("Hello world title");
bigText.setSummaryText("Hello world summary");
mBuilder.setStyle(bigText);
mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent (getApplicationContext(),
MainActivity.class);
// The stack builder object will contain an artificial back
// stack for
// the
// started Activity.
// getApplicationContext() ensures that navigating backward from
// the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder
.create(getApplicationContext());
// Adds the back stack for the Intent (but not the Intent
// itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the
// stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(100, mBuilder.build());
I'm using example code to create a notification, which I then want to handle in onNewIntent() if my App is already running. However, onNewIntent() was not being called. I searched for a few hours and no one seemed to have a proper answer--just some workarounds.
I had to search forever to find the solution here: Android OnNewIntent not called and the answer is not actually explained.
Question
Can anyone explain why we need the lines:
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
in order to receive the notification through onNewIntent()? The App launches just fine without them, but will always go through onCreate() instead.
Example Code
public void createNotification(String s){
// The id of the channel.
String CHANNEL_ID = "my_channel_01";
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon_missing)
.setContentTitle(getString(R.string.notification_channel_name))
.setContentText(R.string.text);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your app to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mNotificationId is a unique integer your app uses to identify the
// notification. For example, to cancel the notification, you can pass its ID
// number to NotificationManager.cancel().
Notification n = mBuilder.build();
n.flags = n.flags | Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(1, n);
}
I am learning Android's notification system, but I have a problem. I am using this code for issue a notification:
private void notBuild() {
int mId=1;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("My notification")
.setAutoCancel(true)
.setSmallIcon(R.drawable.twitter)
.setContentText("Hello World!")
.setNumber(15);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("sss","tolgay007");
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
}
If I issue a notification and then if I press the home button (I mean pausing app) when I return the app(resuming app) main activity is reloading and all variables is returning null. But If I not issue a notification, there is a no problem. I don't want to reload everything if I issue a notification.
How can I do this ?
Ps:I want to run main activity from notification but I don't want to reload just resume it.
Try this
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Please excuse my English, I'm French :
I've got a question about android notifications...
There is an inbox on my app and user is notified when he received a new notified.
When he click on the notification, I want to open the right activity with the right conversation so I've to pass an ID.
I've made a lot of search but I can't find...
Coul'd you help me please ?
Here my code :
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(text);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, LandingActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_CANCEL_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
Thanks.
In this case, you could save the ID of the conversation in the Intent that will launch your app. You do this by using extras:
resultIntent.putExtra("conversationID", "234553");
Then, in your MainActivity, you handle the case where the user has clicked on your notification:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("conversationID");
//Display conversation
}
I don't know if I get your question right: If you want to start different Activities according to something you received, you have to make different Intents for those cases.
If you always want to open your LandingActivity and pass data to it, add them as extras to your Intent, see How do I pass data between Activities in Android application?.
How do I launch an activity when the user clicks on the notification? I am having constant crashes. I can not get the click on the notification to work. There is an onclick method shown below. When a button called ButtonOne is pressed, it will launch a notification in the top menu bar of the screen. I wanted to user to be able to press the notification and have it launch the activity called MainActivity. It crashes and will not launch the page. There is probably something wrong in my code for notification that I put inside of the MainActivity class. What is wrong?
ButtonOne.setOnClickListener(new View.OnClickListener() {
private int mId;
// anonymous inner class override for on click
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, CoverFlowExample.class);
MainActivity.this.startActivity(myIntent);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(MainActivity.this, MainActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
}
});
Found out that the problem was the AVD android emulator not the code. For some reason it did not update the code from earlier version. Tested it again and now it runs with no errors.