i have created a cloud function which sends data with the notifications. Now i want to open always the specific activity. Unfortunately its opening always the MainActivity. Here is my Code of my Firebase Service:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> dataSet = remoteMessage.getData();
if (!dataSet.get("kind").equals("0")) {
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);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.baseline_send_24)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}else{
Intent intent = new Intent(this, SettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.baseline_send_24)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
I dont know if i have to define it somewhere else. Thanks
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction("ACTION.HOME_ACTION");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
and set this pendingintent in notification
.setContentIntent(pendingIntent)
Incase you are using Firebase for your push notifications. If you use the 'notification' type payload, while sending from server, Firebase manages the display of notification for your app without giving a callback and by default will always open the launcher activity of your app.
You can send extra data from the server side, access that data in your launcher activity getIntent methods and redirect to some other activity as needed
Related
I have to send a URL in notification and I want upon clicking the notification, the link will be opened immediately by Chrome. Currently, when I click the notification, it only opens my app.
What changes do I have to implement in this code for my desired behavior?
private void sendNotification(String messageBody, String title) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
When I send notification and app does not open already,I cant see my notification on listview in mainactivity.When I send notification and app already is open,I see on listview.How can I see notifications on listview everytime I send notification?
my service
private void sendNotification(String messageBody) {
SharedPreferences a=getSharedPreferences("noti",MODE_PRIVATE);
SharedPreferences.Editor editor=a.edit();
editor.putInt("i",a.getInt("i",0)+1);
editor.putString(String.valueOf(a.getInt("i",0)+1),messageBody);
editor.apply();
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);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.farmer)
.setContentTitle("Zengin Çiftçi")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
my mainactivity
a=getSharedPreferences("noti",MODE_PRIVATE);
for(int i=a.getInt("i",0);i>0;i--){
liste.add(a.getString(String.valueOf(i),""));
}
I need to call some methods after clicking the notification. My notification has a pendingIntent that opens a browser. But I also have to do some other things AFTER user clicks it. Oh, and I must admit that I get my notifications from GCM(push).
Intent intent;
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlString));
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(mBitmap)
.setContentTitle(title)
.setDefaults(Notification.DEFAULT_ALL)
.setContentText(body)
.setAutoCancel(true);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification myNotification = notificationBuilder.build();
myNotification.setLatestEventInfo(getBaseContext(), title, body, pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(TAG, myNotification);
To handle the click on the notification itself (not an action) you need to specify a ContentIntent as following:
notificationBuilder.setContentIntent(PendingIntent.getActivity(
context,
0,
new Intent(context, YourActivity.class)
.putExtra("extra_something", something)
.setData(Uri.parse("pass an uri if needed")),
PendingIntent.FLAG_UPDATE_CURRENT
))
Hope it helps.
Nothing happens on clicking Accept or Reject button of the head-ups notification.
But when the head-up notification disappear and from clicking Accept and Reject from the notification panel is working.
Testing on Android 5.1.0.
Intent acceptIntent = new Intent(this, NotificationReceiver.class);
acceptIntent.setAction("com.android.test.Accept");
PendingIntent acceptPendingIntent = PendingIntent.getBroadcast(TestApplication.getAppContext(), 12345, acceptIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Intent rejectIntent = new Intent(this, NotificationReceiver.class);
rejectIntent.setAction("com.android.test.Reject");
PendingIntent rejectPendingIntent = PendingIntent.getBroadcast(TestApplication.getAppContext(), 12345, rejectIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.fundu);
builder.setContentTitle("Test Notification");
builder.setContentText("Hello");
builder.setAutoCancel(true);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setCategory(NotificationCompat.CATEGORY_SERVICE);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.addAction(R.drawable.ic_check_icon, "Accept", acceptPendingIntent);
builder.addAction(R.drawable.ic_action_close, "Reject", rejectPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
Just setting the vibration, makes it working fine.
builder.setVibrate(new long[0]);
The key is to call setContentIntent on the Notification Builder and passing it a PendingIntent. The Full code with comments explaining each step is included below. See the part named "THIS IS THE PERTINENT PART" (The Full code is included for completeness sake.)
// Use Notification Builder to start things off
// There are other ways of acquiring a Notification Builder; this is just an example
String channelId = "channelId";
String title = "title";
String body = "body";
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId);
notificationBuilder
.setSmallIcon(R.drawable.ic_alarm)
.setContentTitle(title)
.setContentText(body);
//--------------- THIS IS THE PERTINENT PART ---------------
// Prepare Intent for creating PendingIntent
Intent intent = new Intent(context, ActivityToStart.class);
// Create Pending Intent
int requestCode= 1234; // requestCode has to be a unique ID for EACH PendingIntent
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(
requestCode,
PendingIntent.FLAG_UPDATE_CURRENT // use to prevent re-using current Activity Intent
);
notificationBuilder.setContentIntent(pendingIntent);
// Finally, create the Notification
Notification notification = notificationBuilder.build();
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);
}