show a dialogfragment using notification - java

I have set up a notification like this in my .MainActivity class:
Intent downloadUpdateIntent = new Intent(mContext, updateAppDialogFragment.class);
PendingIntent downloadAppPendingIntent = PendingIntent.getActivity(mContext,0,downloadUpdateIntent,0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Update Available")
.setContentText("A new version of this app is available.")
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(0,"Update",downloadAppPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(mContext.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
What i need is to, when i click on update button, it should show updateAppDialogFragment to show an updating progress. When i generally use this class, i need to make such a call updateAppDialogFragment.newInstance(). But here, I cant understand how to accomplish this. Anyone to help?
I have very basic knowledge about intent. Actually, what i need to know is to use the intent to call a method.

Related

android - notifications hide when I look the screen

it's very weird and it happens just for my app. When I turn off my phone screen and turn it on again, it clears all of my notification despite it doesn't happens for other apps on my phone.
this is my code:
Intent resultIntent = resultIntent = new Intent(mContext, intentClass);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
resultIntent.putExtra("onvan", title);
resultIntent.putExtra("message", message);
resultIntent.putExtra("link", link);
resultIntent.putExtra("linktype", linktype);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder TSB = TaskStackBuilder.create(mContext);
TSB.addParentStack(intentClass);
TSB.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
TSB.getPendingIntent(
new Random().nextInt(),
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent);
I don't want to clear the notification when the screen turns off, I want it to clear when use manually clears it
what is wrong with my code?
You're using
setAutoCancel(true)
As per documentation
setAutoCancel(boolean autoCancel)
Make this notification automatically
dismissed when the user touches it.
I would try setting auto cancel to false.

How to show just notification count on appview android

I'm new to android. And to show notification on android phones I got this below code
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);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.mytune_default)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
notificationManager.notify(m /*ID of notification*/ , notificationBuilder.build());
But the thing is I just want that notification unread count on code appview on system, but dont want other things loke message, title etc that appear on notification tray of system. How come this be acheived>
It's not possible to replace launcher icon with widget. You can say to user to place your widget on the home screen, but not in application list. Users must do that in their own. Nevertheless, it's iOs pattern. It's not good idea to bring iOS design elements to android. Android have great notification bar, where user expect to see notification from your app. Android users don't expect to see it over the icon.

Notifications in Android

So I tried to make notification
Intent resultIntent = new Intent(
this, NavigationActivity.class
);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT
);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("Message received")
.setContentText("Message body")
.setContentIntent(resultPendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
And it doesn't appear. Just nothing happen, no error or something. The log is empty. Android 6.0. The icon resource is exists.
Is there a way to know for sure what is the problem? Where to look for the error message?
Try to use NotificationCompat.Builder instead of Notification.Builder. For more information look at link.
Wow, I'm sooooo idiot. I had the system settings switch on, that blocks all the notifications for the app. Sorry for that.

Using PendingIntent to reach my method

I have an android App that makes notifications. I can generate a notification together with a button, without a problem. My problem is the button's action. I want the button to call a method, for example a text-printing method. I'm using addAction(icon, "title", pendingIntent).
public void sendNotification(Context context) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context.getApplicationContext(), SomeClass.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), (int) System.currentTimeMillis(), intent, 0);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
// This creates the button and it is using a pending intent.
.addAction(R.drawable.icon, "Print something", printPendingIntent)
// Clicking on notification takes you back to the App.
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setContentTitle("My Title")
.setContentText("Some text");
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
So the problem is in printPendingIntent, (no code written as you can see),
how do I access / call a method by using this pending intent?
Thanks for answers and sorry if the question is not clear and detailed enough.
add intent.putExtra("key","printmethod"); then receive the data in your SomeClass using getIntent().getExtras("Key"); if the value is matched then you can call your desired method in that activity.

Show notification in Android 5.0.1

I want to create a Notification in my Android App, but my code doesn't work!
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder nBuilder = new Notification.Builder(this);
nBuilder.setContentTitle("Title...");
nBuilder.setContentText("Text...");
nBuilder.setContentInfo("Info?");
nBuilder.setWhen(System.currentTimeMillis());
//Intent notificationIntent = new Intent(this, MainActivity.class);
//PendingIntent pendingIntent = new PendingIntent.getActivity(this, 0, notificationIntent, 0);
nManager.notify(noti_ID, nBuilder.build());
Thanks :)
From developer.android.com
Required notification contents
A Notification object must contain the following:
A small icon, set by setSmallIcon()
A title, set by setContentTitle()
Detail text, set by setContentText()
So set a small icon too.

Categories