The redirection works fine, but the redirection does not happen if the Push Notification is clicked immediately like within 2 seconds or so. The second time I made sure to wait anywhere around 10-15 seconds and that time it worked fine.
Can anyone help me out how to solve this problem.
Notification notification;
Intent notificationIntent = BMSNotificationIntent.getNotificationIntent(notificationData, mContext,
mSharedPreferencesManager, true);
notificationIntent.putExtras(rawNotificaionData);
PendingIntent NotificationPendingIntent = PendingIntent.getActivity(mContext, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext);
notificationBuilder.setPriority(Notification.PRIORITY_MAX);
notificationBuilder
.setSmallIcon(R.drawable.status_icon)
.setColor(ContextCompat.getColor(mContext, R.color.transparent))
.setContentIntent(NotificationPendingIntent)
.setTicker(notificationData.getCleverTapTitle())
.setAutoCancel(isAutoCancelable())
.setContentTitle(notificationData.getCleverTapTitle())
.setContentText(notificationData.getCleverTapMessage())
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE | NotificationCompat.DEFAULT_SOUND);
notificationBuilder.setDefaults(Notification.DEFAULT_SOUND);
notificationBuilder.setChannelId(NotificationChannelName.GENERAL);
/*Create a Delete Intent that will be called when user removes the notification by swiping or by clear*/
notificationBuilder.setDeleteIntent(getNotificationDeletePendingIntent());
notification = notificationBuilder.build();
notification.flags |= NotificationCompat.FLAG_AUTO_CANCEL;
if (BMSUiUtility.isNotificationFromInbox(rawNotificaionData, mSharedPreferencesManager)) {
mNotificationManager.notify(notificationId, notification);
} else {
mNotificationManager.notify(0, notification);
}
Please use below code snip:)
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
else
Intent intent = new Intent(this, SomeActivity.class);
// Creating a pending intent and wrapping our intent
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
// Perform the operation associated with our pendingIntent
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
Related
If there're two my app's notifications at device's system tray, the user choose one notification, the app's opening but all of other notifications is lost the click behavior, it means user cannot click them anymore. How can I prevent this action?
My code:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle(getString(R.string.test))
.setContentText(messageBody);
Intent intent = new Intent(this, NotifActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle = new Bundle();
bundle.putString(NotificationViewActivity.MESSAGE_EXTRA, messageBody);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt(50) + 1, builder.build());
int mId = (int) System.currentTimeMillis();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.imagename)
.setContentTitle("Title")
.setContentText("message")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, activityname.class);
//activityname is the name of activity which you want to launch
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
I detected my root cause in below code
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Because the request code is the same for all notifications, so when we click to the one notification, the other lost their click listener. Fix like below codes
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_ONE_SHOT);
When user Click on Notification, it will open New Activity That's Working fine. But When i Presss Back button it closed Application
What i want ?
When i click on Back button it return MainActivity(Selected Activity )Everytim.
private void generateNotificationNew(Context context, String message,String pushIdmessage) {
Intent resultIntent = null;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getString(R.string.app_name))
.setAutoCancel(true)
.setContentText(message);
resultIntent = new Intent(context,ResultActivity.class);
//Intent resultIntent = new Intent(this, AvailableJobActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
5,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
}
stackBuilder.addParentStack or stackBuilder.addNextIntent may not working properly.
Any Alternative option Thank you.
For generate notification you need to put following code...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Notificatin Title")
.setContentText("message");
Intent notificationIntent = new Intent(this , ResultActivity);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0 , PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify( 0 , builder.build());
And for the handle back event you need to check following things in onBackPressed() in Result Activity
#Override
public void onBackPressed() {
if(this.isTaskRoot())
startActivity(new Intent(this , MainActivity.class));
super.onBackPressed();
}
Now you can redirect to MainActivity from notification started activity..
Enjoy it...:-)
I hace a BroadCast receiver I use to send a notification. Well, the problem is that I tried all the solutions related to this subject in stackoverflow with no success: I can't see anything after clicking on the notification.
Here is my code:
private void sendFinishNotification(Context context, String user_parent){
try {
Log.i(getClass().getSimpleName(), "Sending Notification");
Intent resultIntent = new Intent(context, RatingActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent pIntent = stackBuilder.getPendingIntent((int) System.currentTimeMillis(), PendingIntent.FLAG_UPDATE_CURRENT);
Notification notif = new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_stat_bs)
.setContentTitle(context.getString(R.string.bs_finished))
.setContentText(context.getString(R.string.come_rate_bs))
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notif);
Log.e("CheckingService", "Building Notification");
}
catch (Exception e) {
e.printStackTrace();
}
}
I tried not using TaskStackBuilder and just getting the pending intent with getActivity() (as well as getBroadcast()) with no success.
Maybe there is a problem with the context? I tried using context.getApplicationContext() instead of context, anything changed.
edit: My code worked fine, I was simply giving it the wrong Activity class (multiple APK build project using shared library).
I'm using the following code to display a foreground notification for my Service. For some reason though, it doesn't show me my activity when I click on the notification.
I've checked out various StackOverflow posts and followed accordingly, and even the sample FakePlayer from CommonsWare but to no avail.
protected void updateNotification(String subtitle) {
// The PendingIntent to launch our activity if the user selects this notification
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Show notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_headset_mic)
.setWhen(System.currentTimeMillis())
.setContentTitle("AirWaves: " + G.SETTINGS.deviceName)
.setContentText(subtitle)
.setContentIntent(pendingIntent)
.setOngoing(true);
startForeground(NOTIFICATION_ID, builder.build());
}
Anyone have an idea where I might be going wrong with this?
i have also implement notification consider the following example
public void sendNotification(Context context,String message, String action) {
try {
int icon = R.drawable.notilogoboss;
String title = context.getString(R.string.app_name);
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("message", message);
intent.putExtra("action", action);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logoboss))
.setSmallIcon(icon)
.setContentTitle(title)
.setWhen(System.currentTimeMillis())
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
int notId = UUID.randomUUID().hashCode();
notificationManager.notify(notId, notificationBuilder.build());
} catch (Exception e) {
}
}
Please try the following method to create the pending intent:
Intent intent = new Intent( context, MainActivity.class );
intent.putExtra( "message", message );
intent.putExtra("action", action);
TaskStackBuilder stackBuilder = TaskStackBuilder.create( context );
stackBuilder.addParentStack( MainActivity.class );
stackBuilder.addNextIntent( intent );
PendingIntent pendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT );
notificationBuilder.setContentIntent( pendingIntent );
Try changing the flag for the pending intent to this:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Also, what is the startForeground(NOTIFICATION_ID, builder.build()); method that you are using? can you post that as well for additional help?
I have prepared a simple test app which posts a notification on a button click:
The source code from MainActivity.java creating the notification is displayed below:
Button showButton = (Button) findViewById(R.id.show);
showButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent appIntent = new Intent(mContext, MainActivity.class);
appIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
appIntent.putExtra("my_data", 12345);
String question = getString(R.string.the_question);
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(mContext)
.setContentTitle(question)
.setContentText(question)
.setTicker(question)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
mManager.notify(NOTIFY_ID, notification);
}
});
My question is: how to modify the notification, so that the user is asked a Yes/No question (in this case: "Do you want to open the car?") and - after she selects Yes or No to launch the same app and run a corresponding method in it (in this case: openCar() or closeCar() method).
I probably should use NotificationCompat.Action.Builder - but how exactly?
Also I am not really sure if this code is the correct code for launching an app from notification and what flags should I use:
Intent appIntent = new Intent(mContext, MainActivity.class);
appIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
And finally I wonder if hardcodidng some random number in NOTIFY_ID is the correct way when posting notifications?
Here is a source code I used for notification with Login/Register action.
private void sendNotification(String message, String title) {
try {
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);
Intent secondActivityIntent = new Intent(this, SecondActivity.class);
PendingIntent secondActivityPendingIntent = PendingIntent.getActivity(this, 0 , secondActivityIntent, PendingIntent.FLAG_ONE_SHOT);
Intent thirdActivityIntent = new Intent(this, ThridActivity.class);
PendingIntent thirdActivityPendingIntent = PendingIntent.getActivity(this, 0 , thirdActivityIntent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_3d_rotation_white_36dp)
.setContentTitle(title)
.setContentText(message)
.addAction(R.drawable.ic_lock_open_cyan_600_24dp,"Login",secondActivityPendingIntent)
.addAction(R.drawable.ic_lock_pink_700_24dp,"Register",thirdActivityPendingIntent)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
} catch (Exception e) {
e.printStackTrace();
}
}
To use it: simply call this method sendNotification(String yourMessage, String yourTitle)
e.g. sendNotification("Hello Message", "Hello Title")
Here is a snapshot of the output
Notify user on pending Intent.. an example is here..
public void notifyUser() {
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(HappyActivity.this, NotificationDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// use the flag FLAG_UPDATE_CURRENT to override any notification already
// there
pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification(R.drawable.ic_launcher,
"Question.....?????", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL
| Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND;
notification.setLatestEventInfo(this, "title",
"Explanation of question..", pendingIntent);
// 10 is a random number I chose to act as the id for this notification
notificationManager.notify(10, notification);
}