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
Related
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 have the following code that creates an ongoing notification for my music player
private Notification buildNotification(String title, String artist) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
notificationBuilder.setOngoing(true);
notificationBuilder.setAutoCancel(false);
notificationBuilder.setSmallIcon(R.drawable.ic_stat_playing);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(), MusicPlayer.class), PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
RemoteViews compactNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notfication_custom_layout);
RemoteViews expandedNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_expanded_layout);
Intent previousTrackIntent = new Intent(Music.getContext(), MusicService.class);
previousTrackIntent.setAction(MusicService.ACTION_REWIND);
PendingIntent previousTrackPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, previousTrackIntent, 0);
Intent playPauseTrackIntent = new Intent(Music.getContext(), MusicService.class);
playPauseTrackIntent.setAction(MusicService.ACTION_TOGGLE_PLAYBACK);
PendingIntent playPauseTrackPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, playPauseTrackIntent, 0);
Intent nextTrackIntent = new Intent(Music.getContext(), MusicService.class);
nextTrackIntent.setAction(MusicService.ACTION_SKIP);
PendingIntent nextTrackPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, nextTrackIntent, 0);
Intent stopServiceIntent = new Intent(Music.getContext(), MusicService.class);
stopServiceIntent.setAction(MusicService.ACTION_STOP);
PendingIntent stopServicePendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, stopServiceIntent, 0);
if (mState == State.Playing) {
compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_pause_dark);
expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_pause_dark);
} else if (mState == State.Paused || mState == State.Stopped) {
compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_play_dark);
expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_play_dark);
}
compactNotification.setTextViewText(R.id.notification_base_line_one, title);
compactNotification.setTextViewText(R.id.notification_base_line_two, artist);
expandedNotification.setTextViewText(R.id.notification_base_line_one, title);
expandedNotification.setTextViewText(R.id.notification_base_line_two, artist);
compactNotification.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
compactNotification.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
compactNotification.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_collapse, stopServicePendingIntent);
compactNotification.setOnClickPendingIntent(R.id.notification_base_collapse, stopServicePendingIntent);
//Set the album art.
expandedNotification.setImageViewBitmap(R.id.notification_expanded_base_image, BitmapFactory.decodeResource(getResources(), R.drawable.selena_gomez));
compactNotification.setImageViewBitmap(R.id.notification_base_image, BitmapFactory.decodeResource(getResources(), R.drawable.selena_gomez));
//Attach the shrunken layout to the notification.
notificationBuilder.setContent(compactNotification);
//Build the notification object.
Notification notification = notificationBuilder.build();
//Attach the expanded layout to the notification and set its flags.
if (Build.VERSION.SDK_INT >= 16)
notification.bigContentView = expandedNotification;
notification.flags = Notification.FLAG_FOREGROUND_SERVICE |
Notification.FLAG_NO_CLEAR |
Notification.FLAG_ONGOING_EVENT;
return notification;
}
The notification appears correctly but the controls dont work even thought the controls are well defined.
Is there something else I need to enable or assign to get the controls to work?
I know this answer is 5 years late, but I found that Notification.FLAG_FOREGROUND_SERVICE still shows the controls, but doesn't allow them to be "clickable". I found the same behavior when I showed the notification with startForeground(int,notification);
If I just used the following flags (Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT), then call stopForeground(false); and then nm.notify(int, notification); it works as expected.
The following code is not a problem , but I did not get the results you want:
【Please help me set】
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(mContext);
mNotifyBuilder.setTicker("****: " + TimeUtil.timeLongToString(System.currentTimeMillis()));
mNotifyBuilder.setSound(Uri.parse("android.resource://com.wealoha.social/raw/dd"));
if (mSessionMap.size() <= 0 && mSessionMap.size() <= 0) {
mSessionMap.put(notification.sessionId, 1);
mNoticeMap.put(notification.sessionId, 1);
mNotifyBuilder.setContentText("content");
mNotifyBuilder.setSmallIcon(R.drawable.ic_launcher);
Intent resultIntent = new Intent(mContext, DialogueActivity.class);
Bundle bundle = new Bundle();
bundle.putString(DialogueActivity.TAG, notification.sessionId);
resultIntent.putExtras(bundle);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
stackBuilder.addParentStack(DialogueActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mNotifyBuilder.setContentIntent(resultPendingIntent);
Notification noti = mNotifyBuilder.build();
noti.flags = Notification.DEFAULT_LIGHTS;
Intent deleteIntent = new Intent(mContext, MonitorNoticeColumnClearBroadcast.class);
deleteIntent.setAction(GlobalConstants.AppConstact.MONITOR_NOTICE_COLUMN_CLEAR_BROADCAST);
noti.deleteIntent = PendingIntent.getBroadcast(mContext, 0, deleteIntent, 0);
mNotificationManager.notify(mNoticeMap.get(notification.sessionId), noti);
I think I found the answer,Thank you very much Mike Laren,
I put it in a property to noti.flags = Notification.FLAG_AUTO_CANCEL,
This can be resolved,
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.