I am trying to notify using notification as below: The main activity has the following code, but nothing is showing up.
mEmailSignInButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("Enable Call Permission")
.setContentText("Please Enable or Accept call permission");
int NOTIFICATION_ID = 12345;
Intent targetIntent = new Intent(MainActivity.this, PermissionActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOTIFICATION_ID, builder.build());
save();
}
});
It seems it doesnt work without an icon.
.setSmallIcon(R.drawable.small_icon);
see this post:
Notification Not showing
Related
I am creating a news android app and i want to send a notification when post added to database.i know Google Firebase Console but i want to send notification automatically.i have following codes:
public void sendNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setPriority(Notification.PRIORITY_MAX);
Intent intent = new Intent(MainActivity.this, NewsActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("My Company");
mBuilder.setContentText("New news received.");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(001, mBuilder.build());
}
And using this in Apiservice:
apiService.getNews(new ApiService.OnNewsReceived() {
#Override
public void onReceived(List<News> news) {
sendNotification();
adsesAdapter = new AdsesAdapter(NewsActivity.this, ) );
recyclerView.setAdapter(adsesAdapter);
}
});
This codes just show notification when user is on app... Is there any way to show notification on news received without opening app???
I have tried setAutocancel as well as FLAG_AUTO_CANCEL but none of these work and i have two buttons inside notification which open two diff activities.. kindly help me
public void createNotification(View view) {
Intent yesIntent = new Intent(this, MyDialog.class);
yesIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
yesIntent.putExtra("NOTI_ID", NOTIFICATION_ID);
yesIntent.putExtra("ACTION", 1);
PendingIntent yespIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), yesIntent, 0);
Intent noIntent = new Intent(this, MyDialog.class);
noIntent.putExtra("ACTION", 0);
PendingIntent nopIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), noIntent, 0);
NotificationCompat.Builder noti = new NotificationCompat.Builder (this)
.setContentTitle("New Project Approval")
.setContentText("Project Description")
.setAutoCancel(true)
.setSmallIcon(R.mipmap.bell)
.addAction(R.mipmap.approve_ic, "Approve", yespIntent)
.addAction(R.mipmap.rejecticon, "Reject", nopIntent) ;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.build().flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_ID, noti.build());
}
You have to cancel your notification manually in your activity. Pass the notification ID to your PendingIntent to retrieve it in your Activity and cancel the corresponding notification via the NotificationManager like :
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(yourNotificationId);
A good and full example : How to dismiss notification after action has been clicked
Previously I callied the NotificationManager outside the onCreate() method, which wasn't working. But following the StackOverflow threadr suggested by #bubu I resolved it by calling NotificationManager inside onCreate();
I want to auto cancel my notification when user clicks on notification. The following code works good in all the devices except Android lollipop device. In Lollipop device, notification goes only when user swipes it off.
#SuppressWarnings("deprecation")
public void sendNotification(int id){
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Test")
.setContentText("Jump to next screen")
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, NextActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
resultIntent, 0);
//PendingIntent.FLAG_UPDATE_CURRENT
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// id, if there is a need to update the notification later on.
mNotificationManager.notify(id, mBuilder.build());
Log.v(TAG, "Notification ID value " + id);
//mNotificationManager.cancel(id);
}
What is missing in this code?
Looks good to me. My auto cancel still works on 5.0.2. Let me give you a portion of my code:
public static void notif(int id, String title, String text, Context context, Class activity) {
// get an instance of the NotificationManager service
if (mNotifyMgr == null)
mNotifyMgr = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context, activity);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notifIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.launcher)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(notifIntent);
mNotifyMgr.notify(id, mBuilder.build());
}
modify following:
setAutoCancel(true) -> setAutoCancel(false);
then on action activity do the following
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
if you want to set special condition you can set by
intent.putExtra("key", "value")
I have prepared a simple test app which posts a notification on a button click:
The source code from MainActivity.java creating the notification is displayed below:
Button showButton = (Button) findViewById(R.id.show);
showButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent appIntent = new Intent(mContext, MainActivity.class);
appIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
appIntent.putExtra("my_data", 12345);
String question = getString(R.string.the_question);
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(mContext)
.setContentTitle(question)
.setContentText(question)
.setTicker(question)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
mManager.notify(NOTIFY_ID, notification);
}
});
My question is: how to modify the notification, so that the user is asked a Yes/No question (in this case: "Do you want to open the car?") and - after she selects Yes or No to launch the same app and run a corresponding method in it (in this case: openCar() or closeCar() method).
I probably should use NotificationCompat.Action.Builder - but how exactly?
Also I am not really sure if this code is the correct code for launching an app from notification and what flags should I use:
Intent appIntent = new Intent(mContext, MainActivity.class);
appIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
And finally I wonder if hardcodidng some random number in NOTIFY_ID is the correct way when posting notifications?
Here is a source code I used for notification with Login/Register action.
private void sendNotification(String message, String title) {
try {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Intent secondActivityIntent = new Intent(this, SecondActivity.class);
PendingIntent secondActivityPendingIntent = PendingIntent.getActivity(this, 0 , secondActivityIntent, PendingIntent.FLAG_ONE_SHOT);
Intent thirdActivityIntent = new Intent(this, ThridActivity.class);
PendingIntent thirdActivityPendingIntent = PendingIntent.getActivity(this, 0 , thirdActivityIntent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_3d_rotation_white_36dp)
.setContentTitle(title)
.setContentText(message)
.addAction(R.drawable.ic_lock_open_cyan_600_24dp,"Login",secondActivityPendingIntent)
.addAction(R.drawable.ic_lock_pink_700_24dp,"Register",thirdActivityPendingIntent)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
} catch (Exception e) {
e.printStackTrace();
}
}
To use it: simply call this method sendNotification(String yourMessage, String yourTitle)
e.g. sendNotification("Hello Message", "Hello Title")
Here is a snapshot of the output
Notify user on pending Intent.. an example is here..
public void notifyUser() {
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(HappyActivity.this, NotificationDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// use the flag FLAG_UPDATE_CURRENT to override any notification already
// there
pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification(R.drawable.ic_launcher,
"Question.....?????", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL
| Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND;
notification.setLatestEventInfo(this, "title",
"Explanation of question..", pendingIntent);
// 10 is a random number I chose to act as the id for this notification
notificationManager.notify(10, notification);
}
I have created a custom layout notification using remoteview. Problem I'm facing is, How to autoCancel the notification once the user touches it.
I did try few things, but none is giving desired result.
Find Code below:
RemoteViews remoteView = new RemoteViews(this.getPackageName(), R.layout.notification_layout);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
builder.setSmallIcon(R.drawable.ic_launcher);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, RQST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);
remoteView.setOnClickPendingIntent(R.id.btnNotification, pIntent);
builder.setAutoCancel(true);
builder.setContent(remoteView);
Notification notify = builder.build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notiManager.notify(NOTY_ID, notify);
For others who have a custom layout and want auto cancel to work,
If clicking anywhere on notification is ok, then don't use remoteView.setOnClickPendingIntent. Because any view with OnClick Listener overrides the notification main OnClick Listener.
Use setContentIntent instead:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle("title");
builder.setContentText("contect");
builder.setSmallIcon(#smallicon);
builder.setWhen(#when);
builder.setContent(remoteViews);
builder.setContentIntent(onClickPendingIntent); // use this
builder.setAutoCancel(true);
The way I achieved this is by creating BroadcastReceiver which controls button clicks from Notification. Create something like this :
public class NotificationButtonListener extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int action = intent.getIntExtra("mode", -1);
switch (action) {
// do some things depending on action if there are more stuff to do
}
}
}
Don't forget to add your BroadcastListener to your manifest file:
<receiver android:name=".NotificationButtonListener">
And you can create a helper class for creating and cancelling notification:
public class NotificationHelper {
private static final int NOTIFICATION_ID = 5;
private static NotificationManager mNotificationManager = null;
public static void showNotification(Context context) {
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
contentView.setOnClickPendingIntent(R.id.btn_close, closePendingIntent);
Intent mMainIntent = new Intent(context, MainActivity.class);
mMainIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent mMainPendingIntent = PendingIntent.getActivity(context, 555, mMainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setTicker("Playing")
.setContent(contentView)
.setAutoCancel(false)
.setOngoing(true)
.setSmallIcon(R.drawable.ic_av_play)
.setContentIntent(mMainPendingIntent);
Notification notification = mBuilder.build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
public static void cancelNotification() {
if (mNotificationManager != null) {
mNotificationManager.cancelAll();
}
}
}
And using that class in your NotificationButtonListener you can call NotificationHelper.cancelNotification(); .
Hope this helps you!