BigPictureStyle Notification not working in AndroidX - android

Code:
here is the code to popup notification on button click
notificationbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "noti", Toast.LENGTH_SHORT).show();
Intent i=new Intent(getActivity(),OrderTrack.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(),(int) Calendar.getInstance().getTimeInMillis(),i,0);
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.half_bicycle);
Notification builder=new NotificationCompat.Builder(getActivity(),"1")
.setSmallIcon(R.drawable.half_bicycle)
.setContentTitle("title")
.setContentText("Text")
.setLargeIcon(bitmap)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap)
.bigLargeIcon(null))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.build();
Notification notif = new Notification.Builder(getContext())
.setContentTitle("New photo from ")
.setContentText("subject")
.setSmallIcon(R.drawable.half_bicycle)
.setLargeIcon(bitmap)
.setStyle(new Notification.BigPictureStyle()
.bigPicture(bitmap))
.build();
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity());
notificationManager.notify(101, notif);
});
Here I am trying to use BigPictureSt
yle notification. I hava tried two variation of code. But both doesn't give any result/notification.
All the above code is inside a fragment.Is this a reason why it's not working?
I have set the App style to NoActionBar.Is this a reason why it's not working?
Please answer this questions with a proper solution !!!!
Adding the Following code doesn't solve the problem
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity());
notificationManager.notify(101, notif);

The code is fine and there is nothing wrong with it but most importantly you forgot to show the notification itself i.e you are not using the objects that are created.
So show the notification like this...
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity());
notificationManager.notify(101, notif);//101 is notification id
EDIT:
If you are targetting Android Oreo and above you need to create the channels for notification like this. Use this function to create notification channel and set this channel id on a notification while creating it...
String channelId = "some_channel_id";
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
Complete code:
notificationbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createNotificationChannel(); //Create notification channel
Toast.makeText(getContext(), "noti", Toast.LENGTH_SHORT).show();
Intent i=new Intent(getActivity(),OrderTrack.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(),(int) Calendar.getInstance().getTimeInMillis(),i,0);
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.half_bicycle);
Notification builder=new NotificationCompat.Builder(getActivity(),"1")
.setSmallIcon(R.drawable.half_bicycle)
.setContentTitle("title")
.setContentText("Text")
.setLargeIcon(bitmap)
.setChannelId(CHANNEL_ID) // Channel Id
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap)
.bigLargeIcon(null))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.build();
Notification notif = new Notification.Builder(getContext())
.setContentTitle("New photo from ")
.setContentText("subject")
.setSmallIcon(R.drawable.half_bicycle)
.setLargeIcon(bitmap)
.setChannelId(CHANNEL_ID) // Channel Id
.setStyle(new Notification.BigPictureStyle()
.bigPicture(bitmap))
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity());
notificationManager.notify(101, notif);//101 is notification id
}
});
For more info visit this : https://developer.android.com/training/notify-user/channels

Related

foreground service notification in lockscreen

I'm trying to achieve a foreground service.
my problem is my notification doesn't appear in the lock screen.
I try to set visibility to PUBLIC but it doesn't work.
#RequiresApi(api = Build.VERSION_CODES.O)
private String createNotificationChannel() {
NotificationChannel notificationChannel =
new NotificationChannel(
"channelId",
"channelName",
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("channelDescription");
notificationChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
NotificationManagerCompat service = NotificationManagerCompat.from(getBaseContext());
service.createNotificationChannel(notificationChannel);
return "myChannelId";
}
public void startForeground() {
Intent notificationIntent = new Intent(this, HomeActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
String channelID = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channelID = createNotificationChannel();
}
Notification notification = new NotificationCompat.Builder(this, channelID)
.setContentTitle(title)
.setContentText(content)
.setShowWhen(false)
.setSmallIcon(R.drawable.ic_notification_small_icon)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent)
.build();
startForeground("notificationId", notification);
}
notification.setOngoing(true)
makes the notification visible on your lock screen.
Code
Notification notification = new NotificationCompat.Builder(this, channelID)
.setContentTitle(title)
.setContentText(content)
.setShowWhen(false)
.setSmallIcon(R.drawable.ic_notification_small_icon)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
if that doesn't work check your device settings.
https://stackoverflow.com/a/26932991/9017620

Android Notification with pending URL-Intent - only by clicking the notification

I'm trying to create a pending URL-Intent for a Notification, that is called, if the user clicks the notification.
The problem is that the Intent is being called by creating the notification, so the url is getting called in the web browser immediatly. I want to prevent this, but do not know how?
Here is my code
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.com"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(MyApp.getContext(), 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MyApp.getContext(), "MyApp")
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("my App")
.setContentText("Click here to call the URL")
.setStyle(new NotificationCompat.BigTextStyle().bigText("...."))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
;
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(MyApp.getContext());
notificationManager.notify(getNotificationId(), builder.build());
How can I prevent executing the intent after the "notify" call??
Intent should only be executed, when the user clicks on the Notification.
try this:
public void notification()
{
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse("https://www.google.com/"));
#SuppressLint("WrongConstant") PendingIntent pending =
PendingIntent.getActivity(this, 0, notificationIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
String channelId = getString(R.string.app_name);
NotificationChannel notificationChannel = null;
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(getApplicationContext());
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(channelId, channelId,
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription(channelId);
notificationChannel.setSound(null, null);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new
NotificationCompat.Builder(getApplicationContext(), channelId)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("my App")
.setContentText("Click here to call the URL")
.setStyle(new NotificationCompat.BigTextStyle().bigText("...."))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pending)
.setAutoCancel(true)
;
notificationManager.notify(0, builder.build());
}

create android notification by click in a button

I try to create android notification, when i click in a button but does not work.
this is the code that i write in button
NotificationManager Nm;
public void Notify(View view) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("LOGIN")
.setContentText("You are login successfully")
.setSmallIcon(R.drawable.done);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
As already stated by Ikazuchi, for android versions since android 8 you'll need to add a notification channel. This can be done, as it's shown in the documentation here: https://developer.android.com/training/notify-user/build-notification#java, like this:
createNotificationChannel();
Notification notification = new NotificationCompat.Builder(this, "channelID")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Much longer text that cannot fit one line...")
.build();
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel serviceChannel = new NotificationChannel(
"channelID",
"Channel Name",
importance
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}

Android - notification does not appear in status bar

When starting an IntentService to upload a file in the background, I want to show a notification to the user that the upload is in progress by calling showNotification() from inside my service:
private void showNotification() {
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_cloud_upload_black_24dp)
.setWhen(System.currentTimeMillis())
.setContentTitle("Uploading")
.setContentText("Your upload is in progress.")
.setOngoing(true)
.build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
Now here's my problem: the notification appears on the lock screen, but not in the status bar when the screen is unlocked. What am I missing?
Deployment target is API level 24, so a missing NotificationChannel should not be the cause.
try it :
PendingIntent pendingIntent = PendingIntent.getActivity(getmContext(), 0, new Intent(getmContext(), MainActivity.class), 0);
android.app.Notification pp = new NotificationCompat.Builder(getmContext())
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentText(getMessage())
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getmContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, pp);
This is my code which is working fine with target SDK 25
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setStyle(inboxStyle)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
where NOTIFICATION_ID
public static final int NOTIFICATION_ID = 100;
I feel so dumb. Turns out the notification was displayed all the time, but with a black icon on black background.
Changed the icon colour in the xml from
android:fillColor="#FF000000"
to
android:fillColor="#FFFFFFFF"
and it works like a charm

Android Wear: Timer like notification card on wear device

I'm working on implementation of Pomodoro app for Android Wear.
I want to make similar to standard timer UI/UX, I guess it's implemented using displaying/updating notification with timer as title, so I'm showing notification and updating it periodically from Service:
private void updateNotification() {
Intent stopActionIntent = new Intent(this, PomodoroActivity.class);
stopActionIntent.setAction(PomodoroActivity.ACTION_STOP);
PendingIntent stopActionPendingIntent = PendingIntent.getActivity(this, 0, stopActionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action stopAction = new NotificationCompat.Action.Builder(
R.drawable.ic_stop,
getString(R.string.stop_pomodoro),
stopActionPendingIntent)
.build();
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_pomodoro_timer))
.setContentTitle(convertDiffToTimer(System.currentTimeMillis(), timerDeadlineMs))
.setPriority(Notification.PRIORITY_MAX)
.setOngoing(true)
.extend(new NotificationCompat.WearableExtender().addAction(stopAction))
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, notification);
}
Unpleasure problem with such solution is blinking of notificaiton.
Any ideas how escape blinking? Or maybe other way to achieve target behaviour?
To update ongoing/existing notification -
Use Same Id of Notification in Builder
Use .setOnlyAlertOnce(true)
NotificationCompat.Builder notificationBuilder;
public void generateNotificationForTimer(String timeInString, boolean isFirstTime) {
if (isFirstTime) {
notificationBuilder = new NotificationCompat.Builder(this)
.setStyle(new NotificationCompat.BigPictureStyle())
.setOnlyAlertOnce(true)
.setContentTitle("Timer Notification Demo")
.setContentText("Time - " + timeInString)
.setSmallIcon(R.drawable.common_signin_btn_icon_dark);
NotificationManagerCompat.from(this).notify(110, notificationBuilder.build());
} else {
notificationBuilder.setContentText(timeInString);
NotificationManagerCompat.from(this).notify(110, notificationBuilder.build());
}
}

Categories

Resources