I am trying to post a notification on a button press using this code:
public void onClick(View v) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(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(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(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.
i = (int) Math.random();
mNotificationManager.notify(i, mBuilder.build());
}
and I know the .notfiy(id, notification) allow it to only post one notification with the same ID but just for the sake of this example app i would like to keep being able to post notifications even it it is the same one. Thats why i tried to use a random number so it would switch the id, but that still didnt help. Any suggestions?
by the way this is posting a notification, but only one until i clear it
Use a global int 'i=0' and increment it after every notification creation and change code like following
public void onClick(View v) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(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(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(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);
i++;
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
i,
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(i, mBuilder.build());
}
Hope this will resolve the issue
Related
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 have an application,I am sending notification with this code
private void notBuild() {
int mId=1;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("Message")
.setAutoCancel(true)
.setSmallIcon(R.drawable.twitter)
.setContentText("user01 sent a message")
.setNumber(15);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(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(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());
}
This is starting mainactivity and it's okay.But I want to run a method in the main activity.
Example:You will see in the setContentText user01 sent a message,when user press the notification I want to run startChat(user01) method in the main activity.How can I do it ?
You can put data in the resultIntent variable.
resultIntent.putExtra('key', 'useridhere');
Then, in your Activity (onCreate or OnResume) you can call
String userId = getIntent().getStringExtra('key', null);
if(userId != null)
{
//StartChat(....);
}
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);
when i try to set my Notification code into button
it always gives me the error in this part
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this)
[[ The constructor NotificationCompat.Builder(new View.OnClickListener(){}) is undefined ]]
how i can solve this problem ???
Button button9= (Button) findViewById(R.id.button9);
button9.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
///////////My Notification//////////////////////////
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("I'm astm loooooooool")
.setContentText("Hello baby to my world!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(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(this);
// Adds the back stack for the Intent (but not the Intent itself) (ResultActivity)
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);
int mId = 0;
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
///////////End Notification//////////////////////////
}
});
Thanks guys for helping me [[ No one gives me any answer looooooool ]]
i get the answer with my self
just define the NotificationCompat.Builder as Final and it worked well
^__^
///////// my Nine button (set Notification) //////////
final NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("I'm astm loooooooool")
.setContentText("Hello baby to my world!");
Button button9= (Button) findViewById(R.id.button9);
final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
button9.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
///////////My Notification//////////////////////////
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent();
// 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.
// Adds the back stack for the Intent (but not the Intent itself) (ResultActivity)
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);
int mId = 0;
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
///////////End Notification//////////////////////////
}
});
Change this with getBaseContext
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext());
I had the same problem. I changed:
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this)
with:
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this.context)
I like to launch one of my activities in my application on clicking the notification.
I designed a pending intent as discussed in the link Notifications.
But when i click the notification, an activity is launched but that is not the activity supposed to launch from my application. Just the activity that has same class name as my activity is launched. I set a break point in my activity and the break point is never hit.
What is wrong? NotificationListActivity is the one I like to launch. Now the activity titled with NotificationListActivity is launched, but not my activity.
Intent resultIntent = new Intent(thisclasscontext, NotificationListActivity.class);
resultIntent.putExtra("MOBILENUMBER", tel);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(thisclasscontext);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(NotificationListActivity.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);
You can use the below as a reference it works
public void notification()
{
int mId=1;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(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, SecondActivity.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(SecondActivity.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());
}