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)
Related
When user Click on Notification, it will open New Activity That's Working fine. But When i Presss Back button it closed Application
What i want ?
When i click on Back button it return MainActivity(Selected Activity )Everytim.
private void generateNotificationNew(Context context, String message,String pushIdmessage) {
Intent resultIntent = null;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getString(R.string.app_name))
.setAutoCancel(true)
.setContentText(message);
resultIntent = new Intent(context,ResultActivity.class);
//Intent resultIntent = new Intent(this, AvailableJobActivity.class);
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(
5,
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(0, mBuilder.build());
}
stackBuilder.addParentStack or stackBuilder.addNextIntent may not working properly.
Any Alternative option Thank you.
For generate notification you need to put following code...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Notificatin Title")
.setContentText("message");
Intent notificationIntent = new Intent(this , ResultActivity);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0 , PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify( 0 , builder.build());
And for the handle back event you need to check following things in onBackPressed() in Result Activity
#Override
public void onBackPressed() {
if(this.isTaskRoot())
startActivity(new Intent(this , MainActivity.class));
super.onBackPressed();
}
Now you can redirect to MainActivity from notification started activity..
Enjoy it...:-)
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 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
I want to make my Android notification stay even if user clicks it or clicks clear all...
Right now it stays sometimes, and gets removed sometimes, and I'm not sure what causes it.
Here's my code for the notification:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void createNotification()
{
NotificationManager notificationManager = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle("Wolftech Field Agent")
.setContentText("Getting Status")
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setAutoCancel(false);
Intent intent = new Intent(context, FieldAgent.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(FieldAgent.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
public static void updateNotificationText(String inString)
{
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentText(inString)
.setContentTitle("Wolftech Field Agent")
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setAutoCancel(false);
Intent intent = new Intent(context, FieldAgent.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(FieldAgent.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
public static void cancelNotification()
{
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
}
I think you are looking for ongoing notifications, in that case if you are using NotificationCompat.Builder , you can use :
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this);
mNotifyBuilder.setOngoing(true);
I believe that you are looking for this flag:
Notification.FLAG_NO_CLEAR
Add that flag to your notification.
you try
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notificationManager.cancel(pendingIntent);
Okey, I've realized the code I posted is actually good.
What is happening is that when I'm clicking the notification, it's calling onCreate() again, and then after a random interval it's calling onDestroy().
In onDestroy() I had my cancelNotification() method, so if onDestroy() got called after onCreate(), it removed my notification.
Which brings me to a new question: Why is it destroying and recreating my activity after I've followed every answer I could find on here on how to stop that from happening?
You can find that question here How to make notification resume and not recreate activity? if you want to help me solve it...
I had this issue too. My Solution is the add extra on the function that creates the intent
private void addNotification(String headLine, String info, Context context) {
mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon) //R.drawable.icon
.setContentTitle(headLine)
.setContentText(info)
.setOngoing(true)
.setAutoCancel(false);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, MainActivity.class);
resultIntent.putExtra("FROM_NOTIF", true);
// 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_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
// mBuilder.setf |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(mNotification_id, mBuilder.build());
}
and In MainActivity:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.getBooleanExtra("FROM_NOTIF", false)) {
addNotification(...)
... call the function that adds the notification again ..}
So thing is that the notification turns off after the user press on it, but than you can be notify on the activity that started using onNewIntent(), check the intent over there and if you find out that it came from the notification, set it again.
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());
}