Android - Send notification with id - android

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?.

Related

Why must you use Intent.setAction and Intent.AddCategory to use singleTop with PendingIntent?

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);
}

ViewFlipper inside Remoteview showNext() and showPrevious() not working

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

Trying to restore state of activity upon starting it via a notification

I am implementing a webview loading the URL of an web-application that dynamically adds content to the DOM based on user-action via a socket.
As I am closing the App and a user adds another element in the web-application the app-user gets a notification.
If I re-open the App via manually starting it again, I can easily restore the webview and see the new content as well.
My problem: I want to achieve the same when re-opening the App via the notification. Currently I do not get savedInstanceState in onCreate().
launchMode is set to singleTop
Code in WebAppInterface.java:
System.out.println("WebAppInterface:" + MainActivity.MY_ACTIVITY_STATUS);
if (!MainActivity.MY_ACTIVITY_STATUS) {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Neue Nachricht")
.setContentText(username + " hat dir geschrieben!")
.setAutoCancel(true)
.setSound(alarmSound)
.setVibrate(new long[] { 0, 1000, 1000, 1000, 1000 });
// 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(mContext);
// 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
Intent intent= new Intent(mContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
mBuilder.setContentIntent(resultPendingIntent);
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
Code for restoring in my MainActivity:
if (savedInstanceState != null) {
myWebView.restoreState(savedInstanceState);
} else {
myWebView.loadUrl(URL_TO_WEB_APP);
}
So far I'Ve testes various combinations of Intent.FLAGS/launchMode as well as saving savedInstanceState to a private static variable and trying to restore the webview based on this variable (getting tons of "nativeOnDraw failed; clearing to background color." in LogCat).
Upon starting the App via notification:
private static variable "myWebView" (in which I store the webview) is set
onCreate() parameter Bundle savedInstanceState is null
private static variable "savedState" (above mentioned attempt to safe savedInstanceState and use it) is set
I'd be happy for every hint and/or even a solution, as I am currently completely clueless.
Even though I'm sure I had this combination tested dozens of times, the working combination - in order to restore the state of the WebView - is:
In WebAppInterface.java:
Intent intent= new Intent(mContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
In AndroidManifest.xml:
<activity
...
android:launchMode="singleTask" >
Furthermode there was no code needed for onResume()/onNewIntent() in the MainActivity.

Android notification reloads everything

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);

PendingIntent for TaskStackBuilder

I read the document for creating the notification. In that they have used TaskStackBuilder:
to create seperate task for Activity.
to add the parent of activity using addParentStack()
to add an intent
Eventually to create PendingIntent.
after that they didn't use StackBuilder Object to set in NotificationCompat.Builder object. They have used PendingIntent object.
Are all of the above informations (to create a seperate task, to identify the parent activity, to identify the intent) reside in PendingInent ?
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.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(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
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
When you use notifications in your app what happens is. You first specify an explicit intent(Normal intent). Then you create TaskStackBuilder object to access the activity you wan't to start when notification gets clicked then initialize PendingIntent reference with TaskStackBuilder getPendingIntent() and passes it to notification object.
Now what the PendingIntent do is get the intent from the TaskStackBuilder object using stackBuilder.getPendingIntent(int id, Flag) and passes pending object to notification object by calling notification.setContentIntent(PendingIntent object)
To recover mistakes in your code follow this:-
First you should create explict Intent
Then create TaskStackBuilder object then add the parentStack and nextIntent.
Create PendingIntent object then get it's intent using stackBuilder.getPendingIntent(int id, Flag).
Then pass it to Notification object using notification.setContentIntent(PendingIntent).
I am sure you will be done with this instruction...
Note:- Notification object does not accept Intent Object it requires PendingIntent object.

Categories

Resources