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.
Related
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);
I need to display notification with two buttons. Different operation need to perform for each button.so for that I have written following code but when I'm getting multiple notification delete action is not performing.
Random NOTIFICATION_ID = new Random();
int CANCELNOTIFICATIONID = NOTIFICATION_ID.nextInt();
// define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Log.i("******* Service6", "" + msg);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(GcmIntentService.this, LoginActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(GcmIntentService.this, 0, intent, 0);
Intent deleteIntent = new Intent(GcmIntentService.this, DeleteArchiveLoopActivity.class);
deleteIntent.putExtra(LoopMeConstants.EXTRA_DELETE_ARCHIVE_LOOPS, "Delete loops");
Trace.i(TAG, "Looptype Delete loop");
deleteIntent.putExtra("DELETE_ARCHIVE_LOOP_ID", loopId);
deleteIntent.putExtra("NOTIFICATONID", CANCELNOTIFICATIONID);
deleteIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// PendingIntent pDeleteIntent = PendingIntent.getActivity(this, 145623, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent archiveIntent = new Intent(GcmIntentService.this, DeleteArchiveLoopActivity.class);
Trace.i(TAG, "Looptype Archive loop");
archiveIntent.putExtra(LoopMeConstants.EXTRA_DELETE_ARCHIVE_LOOPS, "Archive loops");
archiveIntent.putExtra("DELETE_ARCHIVE_LOOP_ID", loopId);
archiveIntent.putExtra("NOTIFICATONID", CANCELNOTIFICATIONID);
archiveIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// PendingIntent pArchiveIntent = PendingIntent.getActivity(this, 145623, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
notificationBuilder.setContentTitle("Sample");
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(mPushtext));
notificationBuilder.setContentText(mPushtext);
notificationBuilder.setSound(soundUri);
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, 145623, deleteIntent, PendingIntent.FLAG_ONE_SHOT));
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, 145623, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT));
notificationBuilder.setAutoCancel(false);
// notificationBuilder.setContentIntent(pArchiveIntent);
Log.i("GCMIntent Srevice5", "" + msg);
notificationBuilder.setOngoing(true);
mNotificationManager.notify(CANCELNOTIFICATIONID, notificationBuilder.build());
How to solve this issue.
You need to use different requestCode in Pending intent
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, 1, deleteIntent, PendingIntent.FLAG_ONE_SHOT));
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, 2, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT));
In the below way i have solved it.
in the intent i have given two differentactivities
Intent deleteIntent = new Intent(GcmIntentService.this, DeleteLoopActivity.class);
Intent archiveIntent = new Intent(GcmIntentService.this, ArchiveLoopActivity.class);
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, CANCELNOTIFICATIONID, deleteIntent, 0));
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, CANCELNOTIFICATIONID, archiveIntent, 0));
The above code solves my problem
Thanks all
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();
i want to Change button background on click event , notification bar
for eg. i have button with text play which is on notification bar when i click on play it should be change to pause and again if i click on pause this pause text should be changed to play text, how can we do it any help is appreciated
thanks in advance
it is more like music player has on going notification so we can play and pause the media using notification bar as well.
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews contentv = new RemoteViews(getPackageName(),
R.layout.notification_bar);
int icon = R.drawable.noti_launcher;
CharSequence tickerText = "notification";
long when = System.currentTimeMillis(); // now
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
notification.contentView = contentv;
Intent laucherIntent = new Intent(this, NavigationActivity.class);
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
PendingIntent launcherPI = PendingIntent.getActivity(this, 0,
laucherIntent, 0);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
Intent flaotIntent = new Intent(this, FloatMainActivity.class);
PendingIntent floatPI = PendingIntent.getActivity(this, 0, flaotIntent,
0);
PendingIntent wifiPI = PendingIntent.getBroadcast(context, 0,
new Intent(Constants.ACTION_WIFI), 0);
PendingIntent btPI = PendingIntent.getBroadcast(context, 0, new Intent(
Constants.ACTION_BLUETOOTH), 0);
PendingIntent mobileDataPI = PendingIntent.getBroadcast(context, 0,
new Intent(Constants.ACTION_DATA), 0);
PendingIntent alarmsPI = PendingIntent.getBroadcast(context, 0,
new Intent(Constants.CUSTOM_ACTION), 0);
contentv.setImageViewResource(R.id.bluetooth, R.drawable.noti_wifi);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
contentv.setOnClickPendingIntent(R.id.bluetooth, btPI);
contentv.setOnClickPendingIntent(R.id.wifi, wifiPI);
contentv.setOnClickPendingIntent(R.id.data, mobileDataPI);
contentv.setOnClickPendingIntent(R.id.alarm, pi);
contentv.setOnClickPendingIntent(R.id.icon, launcherPI);
contentv.setOnClickPendingIntent(R.id.float_notification, floatPI);
notificationManager.notify(1, notification);
contentv.setInt(R.id.viewId, "change", R.drawable.nextId);
you can use this for change background image.
Hello guys Im using advance notification manager this is the code
Notification note = new Notification();
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notif);
contentView.setImageViewResource(R.id.image, R.drawable.notif);
contentView.setTextViewText(R.id.title, "Focused Crawling");
contentView.setTextViewText(R.id.text, "Crawling In Progress....");
note.contentView = contentView;
Intent notificationIntent = new Intent(this, LoadingActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
note.contentIntent = contentIntent;
note.vibrate = new long[] { 500L, 200L, 200L, 500L };
note.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "2");
note.flags |= Notification.FLAG_NO_CLEAR;
mgr.notify(1337, note);
startForeground(1337, note);
and why it didnt appear at the status bar ?
You don't specify an icon for the status bar, which is required.
Add a line like this to your code:
note.icon = R.drawable.youricon;
See Creating a Notification