Cannot resolve setLatestEventInfo the sdk version i am using is 23 - android

Notification notification = new Notification(R.drawable.stat_sample,
getText(R.string.new_friend_request_exist),
System.currentTimeMillis());
Intent i = new Intent(this, UnApprovedFriendList.class);
i.putExtra(FriendInfo.FRIEND_LIST, tmp);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
i, 0);
notification.setLatestEventInfo(this, getText(R.string.new_friend_request_exist),
"You have new friend request(s)",
contentIntent);
What is wrong in the above code?.............................................................................................................................................

ya because it is deprecated method.
you can use
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
myNotification = builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.yourstring)
.setTicker(notificationText).setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(getText(R.string.title))
.setContentText(notificationText).build();

Related

Starting Activity from a notification - Android Studio

I am trying to start an activity from a notification. Upon starting that activity, I add data via intent.putextra to the intent so the activity shows the right content for the situation.
The activity that is being started is supposed to be open only once in the stack. I did achieve this via
android:launchMode="singleTop"
in my manifest.
However - and now I come to my question - if this activity is already running, I want it to close and replace it with the instance I am creating with the specific additional data (put extra). How can I achieve this?
Heres the code of my notification:
public void newMessageNotification(String title, String message, String otherusernumber, String requestStatus, String sendername) {
notificationManager = NotificationManagerCompat.from(this);
Intent chatIntent = new Intent(this,ChatActivity.class);
//these three strings define the behaviour of the chat activtity
chatIntent.putExtra("otherUsername", sendername);
chatIntent.putExtra("otherUsernumber", otherusernumber);
chatIntent.putExtra("requestStatus", requestStatus);
chatIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), chatIntent,
PendingIntent.FLAG_UPDATE_CURRENT);;
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_new_message)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.build();
notificationManager.notify(1, notification);
}
Try below code
Add PendingIntent in notification
Intent intent = new Intent(this, OpenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Full create notification code
Intent intent = new Intent(this, OpenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager =
(NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);
Uri defaultSoundUri = RingtoneManager . getDefaultUri (RingtoneManager.TYPE_NOTIFICATION);
Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// The id of the channel.
String Ch_id = "yourappname_01";
// The user-visible name of the channel.
CharSequence name = "Notification";
// The user-visible description of the channel.
//String description = getString(R.string.channel_description);
int importance = NotificationManager . IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(Ch_id, name, importance);
mChannel.setSound(defaultSoundUri, new AudioAttributes . Builder ().build());
notificationManager.createNotificationChannel(mChannel);
// Create a notification and set the notification channel.
notification = new Notification . Builder (this, Ch_id)
.setSmallIcon(R.drawable.notify)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(remoteMessage.getData().get("title"))
.setAutoCancel(true)
.setContentIntent(pendingIntent) //Add PendingIntent
.setChannelId(Ch_id)
.build();
} else {
// Create a notification
notification = new Notification . Builder (this)
.setSmallIcon(R.drawable.notify)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(remoteMessage.getData().get("title"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent) //Add PendingIntent
.build();
}
//Generate Diff Notification
int m =(int)((new Date ().getTime() / 1000L) % Integer.MAX_VALUE);
notificationManager.notify(m, notification);
Update
Intent intent = new Intent(this, OpenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_ONE_SHOT);
I hope this can help you!
Thank You.
Try to add flag: Intent.FLAG_ACTIVITY_NEW_TASK to your Intent
Try Below
Intent intent = new Intent(this, ActivityDestination.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
//pendingIntent = PendingIntent.getActivity(this, 0,intent,PendingIntent.FLAG_ACTIVITY_NEW_TASK);

Need to make Notification display always on top of notification bar, like a music player

I have tried below code for it with custom Notification but its not working. I want to display counter in my custom notification like music player .
final RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.layout_workout);
remoteViews.setTextViewText(R.id.layout_workout_tv_name, type + " " + videoList.get(position).getName());
remoteViews.setTextViewText(R.id.layout_workout_tv_time, strTime);
final Intent pauseWorkout = new Intent();
pauseWorkout.setAction(ACTION_PAUSE);
final PendingIntent pauseWorkoutPendingIntent = PendingIntent.getBroadcast(this, 101, pauseWorkout, PendingIntent.FLAG_UPDATE_CURRENT);
final Intent resumeWorkout = new Intent();
resumeWorkout.setAction(ACTION_RESUME);
final PendingIntent resumeWorkoutPendingIntent = PendingIntent.getBroadcast(this, 102, resumeWorkout, PendingIntent.FLAG_UPDATE_CURRENT);
final Intent skipWorkout = new Intent();
skipWorkout.setAction(ACTION_SKIP);
final PendingIntent skipExercisePendingIntent = PendingIntent.getBroadcast(this, 103, skipWorkout, PendingIntent.FLAG_UPDATE_CURRENT);
final Intent completeSetWorkout = new Intent();
completeSetWorkout.setAction(ACTION_COMPLETE_SET);
final PendingIntent completeSetPendingIntent = PendingIntent.getBroadcast(this, 104, completeSetWorkout, PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(getNotificationIcon())
.setColor(ContextCompat.getColor(this, R.color.colorAccent))
.setAutoCancel(false)
.setContent(remoteViews)
.setShowWhen(false)
.setOngoing(true);
notificationBuilder.setWhen(System.currentTimeMillis());
remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_complete_set, completeSetPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_pause, pauseWorkoutPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_resume, resumeWorkoutPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_skip, skipExercisePendingIntent);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
I have also tried below code as well
.setPriority(Notification.PRIORITY_MAX)
For the higher priority notifications, use below code snippet...
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.some_small_icon)
.setContentTitle("Title")
.setContentText("This is a test notification with MAX priority")
.setPriority(Notification.PRIORITY_MAX);

Open application after clicking on notification Android

I want to run my application after notification click!
I want to run: app-->com.zaglebiefm.Radio
This file diectory: library-->com.zaglebiefm.library.Notyfication
Notification.class:
private void buildNotification() {
Intent intentPlayPause = new Intent(NOTIFICATION_INTENT_PLAY_PAUSE);
Intent intentOpenPlayer = new Intent(NOTIFICATION_INTENT_OPEN_PLAYER);
Intent intentCancel = new Intent(NOTIFICATION_INTENT_CANCEL);
PendingIntent playPausePending = PendingIntent.getBroadcast(this, 23, intentPlayPause, 0);
PendingIntent openPending = PendingIntent.getBroadcast(this, 31, intentOpenPlayer, 0);
PendingIntent cancelPending = PendingIntent.getBroadcast(this, 12, intentCancel, 0);
RemoteViews mNotificationTemplate = new RemoteViews(this.getPackageName(), R.layout.notification);
Notification.Builder notificationBuilder = new Notification.Builder(this);
if (artImage == null)
artImage = BitmapFactory.decodeResource(getResources(), R.drawable.default_art);
mNotificationTemplate.setTextViewText(R.id.notification_line_one, singerName);
mNotificationTemplate.setTextViewText(R.id.notification_line_two, songName);
mNotificationTemplate.setImageViewResource(R.id.notification_play, isPlaying() ? R.drawable.btn_playback_pause : R.drawable.btn_playback_play);
mNotificationTemplate.setImageViewBitmap(R.id.notification_image, artImage);
mNotificationTemplate.setOnClickPendingIntent(R.id.notification_play, playPausePending);
Notification notification = notificationBuilder
.setSmallIcon(smallImage)
.setContentIntent(openPending)
.setPriority(Notification.PRIORITY_DEFAULT)
.setContent(mNotificationTemplate)
.setUsesChronometer(true)
.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
RemoteViews mExpandedView = new RemoteViews(this.getPackageName(), R.layout.notification_expanded);
mExpandedView.setTextViewText(R.id.notification_line_one, singerName);
mExpandedView.setTextViewText(R.id.notification_line_two, songName);
mExpandedView.setImageViewResource(R.id.notification_expanded_play, isPlaying() ? R.drawable.btn_playback_pause : R.drawable.btn_playback_play);
mExpandedView.setImageViewBitmap(R.id.notification_image, artImage);
mExpandedView.setOnClickPendingIntent(R.id.notification_expanded_play, playPausePending);
notification.bigContentView = mExpandedView;
}
if (mNotificationManager != null)
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
I tried some things but they won't work for me.
Any ideas or solutions.
Use the below code instead to launch desired/targetted application activity.
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context,YourTargetActivity.class), 0);
mBuilder.setContentIntent(pendingIntent);
This should work.
Intent myintent = new Intent(this, Splash_Activity.class);
myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
int randomPIN = (int)(Math.random()*9000)+1000;
PendingIntent conIntent = PendingIntent.getActivity(this, randomPIN,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(conIntent);
This will work

Notification inbox style however gets updated when a notification comes

I am trying to make a notification style where I want all notification would under one hood and they will be shown line wise. If a new notification comes, it should be under the second latest. The list of notification should be of 5, that means only latest 5 will be displayable.
I am using this below code for this, I don't know how to achieve this, I tried my hands StackBuilder too however I got that, this would work from higher API than I am using one now.
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, GcmActivity.class);
notificationIntent.putExtra("title", messagetype);
notificationIntent.putExtra("message", msg);
PendingIntent intent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("MY MESSENGER").setContentText("MESSAGES");
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("MESSAGES" + " Details");
inboxStyle.addLine(messagetype + ":" + msg);
mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(intent);
mBuilder.setAutoCancel(true);
notificationManager.notify(Integer.parseInt("0"), mBuilder.build());
I know I can do this by adding any number of line while creating
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
However I want that when ever my GCMIntentService will be called, this list will be updated.
I tried the work using this below code as well however that did not work either
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, GcmActivity.class);
notificationIntent.putExtra("title", messagetype);
notificationIntent.putExtra("message", msg);
PendingIntent intent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setAutoCancel(true)
.setContentTitle("MY MESSENGER")
.setSmallIcon(R.drawable.ic_launcher)
.setContentText("MESSAGES");
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
for(int j= 0; j<listMessage.size(); j++)
inboxStyle.addLine(listMessage.get(j));
mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(intent);
notificationManager.notify(0, mBuilder.build());
I had the same issue and fixed it by persisting the notifications on the database to load them when a new one arrives, and delete them on click or delete.
DatabaseHelper dbHelper = new DatabaseHelper(this);
Cursor notifications = dbHelper.getAllNotifications();
NotificationCompat.Builder mBuilder = extractNotifications(title, msg, contentIntent, notifications);
dbHelper.insertNotification(title + ": " + msg);
private NotificationCompat.Builder extractNotifications(String title, String msg, PendingIntent contentIntent, Cursor notifications) {
NotificationCompat.Builder mBuilder;
mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_icon)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(NOTIFICAITONS))
.setContentText(msg)
.setAutoCancel(true)
.setLights(Color.WHITE, 1000, 5000)
.setDefaults(Notification.DEFAULT_VIBRATE |
Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
.setContentIntent(contentIntent);
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(NOTIFICAITONS);
while (notifications.moveToNext())
{
inboxStyle.addLine(notifications.getString(notifications.getColumnIndex(DatabaseHelper.NOTIFICATION_MESSAGE)));
}
inboxStyle.addLine(title + ": " + msg);
mBuilder.setStyle(inboxStyle);
return mBuilder;
You need to do that yourself, by assigning own ID to your notification you will be able to update it later. And you need to build the notification yourself too, by concatenating messages

Android custom notification bar

I do own according to the notification bar: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
I do like the tutorial :
Notification notification = new Notification();
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.icon = icon;
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.custom_notification);
contentView.setImageViewResource(R.id.image, R.drawable.b_10);
contentView.setTextViewText(R.id.title, "Custom zgłoszenie");
contentView.setTextViewText(R.id.text, "test test test");
notification.contentView = contentView;
NotificationIntent Intent = new Intent(BatteryInfoService.this,
BatteryInfoActivity.class);
ContentIntent PendingIntent = PendingIntent.getActivity(ta, 0,
notificationIntent, 0);
notification.contentIntent = contentIntent;
mNotificationManager.notify(BATTERY_ID, notification);
There are errors in the lines:
NotificationIntent Intent = new Intent(BatteryInfoService.this,
BatteryInfoActivity.class);
ContentIntent PendingIntent = PendingIntent.getActivity(ta, 0,
notificationIntent, 0);
notification.contentIntent = contentIntent;
Errors:
NotificationIntent cannot be resolved to a type
Multiple markers at this line
- ContentIntent cannot be resolved to
a type
- ta cannot be resolved to a variable
contentIntent cannot be resolved to a variable
Replace NotificationIntent with Intent (android does not provide NotificationIntent, unless its a custom class).
what you are looking for is
Intent notificationIntent = new Intent(BatteryInfoService.this,BatteryInfoActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Please read the tutorial carefully. Intent and PendingIntent are android Classes where as NotificationIntent and ContentIntent are not. If you have made custom classes under those names and you can import the appropriate packages.

Categories

Resources