Reputation: 334
Currently i am working on push notification in my project. When push notification show i want to open Mynotification activity when click on the notification but it always open MainActivity why? I also google and SO for that but can't find right answer.
Here is my MyFirebaseMessagingService:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
sendNotification(remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MyNotification.class);
intent.putExtra("Message",messageBody);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
0);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Shri Sidhbali Baba")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
And here is my manifest file:
<activity android:name=".MyNotification"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name=".MyNotification" android:label="@string/app_name"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
How i can open Mynotification activity when clicking oh push notification. Thanks for your time...
Upvotes: 2
Views: 768
Reputation: 1469
Try this one
public class VideoNotification extends AppCompatActivity {
public Bitmap image ,bmp;
public NotificationCompat.Builder nb;
final String url = "https://www.google.es/images/srpr/logo11w.png";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_notification);
}
public void createNotification(View view) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, VideoActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
nb = new NotificationCompat.Builder(this);
nb.setSmallIcon(R.drawable.icon);
nb.setContentTitle("Image Notification");
nb.setContentText("Set Content text");
nb.setTicker("Set Ticker text");
// nb.setStyle(new NotificationCompat.BigTextStyle().bigText("Big View Styles"));
/* try {
URL url = new URL("https://www.gstatic.com/webp/gallery3/1.png");
image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
} */
Glide.
with(this).
load(url).
asBitmap()
.centerCrop()
.into(new SimpleTarget<Bitmap>(200,200) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
NotificationCompat.BigPictureStyle bps = new NotificationCompat.BigPictureStyle().bigPicture(resource);
bps.setSummaryText("Summary text appears on expanding the notification");
nb.setStyle(bps);
}
});
// Width and height
// NotificationCompat.BigPictureStyle bps = new NotificationCompat.BigPictureStyle().bigPicture(bmp);
// bps.setSummaryText("Summary text appears on expanding the notification");
// nb.setStyle(bps);
/* NotificationCompat.BigPictureStyle bps = new NotificationCompat.BigPictureStyle().bigPicture(bmp);
bps.setSummaryText("Summary text appears on expanding the notification");
nb.setStyle(bps); */
// Bitmap bitmap_image = BitmapFactory.decodeResource(this.getResources(), R.drawable.picture);
// NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle().bigPicture(bitmap_image);
// s.setSummaryText("Summary text appears on expanding the notification");
// nb.setStyle(s);
TaskStackBuilder TSB = TaskStackBuilder.create(this);
TSB.addParentStack(VideoNotification.class);
// Adds the Intent that starts the Activity to the top of the stack
TSB.addNextIntent(intent);
PendingIntent resultPendingIntent =
TSB.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
nb.setContentIntent(resultPendingIntent);
nb.setAutoCancel(true);
nb.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
nb.setSound(alarmSound, AudioManager.STREAM_MUSIC);
NotificationManager mNotificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(11221, nb.build());
}
}
Upvotes: 0
Reputation: 3015
AFAIK, this piece of code runs when the app is in the foreground. When it is in the background, Firebase delivers the notification to the system tray. I had faced a similar issue. When it delivers to system tray it was always opening the MainActivity. I had noticed that firebase allows to pass arguments to the Intent. I had leveraged this technique to read the custom string (Open advance options from firebase message console and specify key as 'item' and value as 'MyNotification'). Read this string from the main activity and redirect the control to the appropriate activity from your MainActivity::onCreate() method
//private String ITEM = "item";
@Override
protected void onCreate(Bundle savedInstanceState) {
//Parse the input passed to the activity
Intent intent = getIntent();
String input = intent.getStringExtra(ITEM);
//Redirect to MyNotification
Intent redirect = new Intent(this, MyNotification.class);
startActivity(redirect);
}
Upvotes: 2
Reputation: 744
update this :
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MyNotification.class);
intent.putExtra("Message",messageBody);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MyNotification.class), 0);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Shri Sidhbali Baba")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder.setContentIntent(contentIntent);
notificationManager.notify(0, notificationBuilder.build());
}
Upvotes: 1
Reputation: 94
try to remove this line in your Manifest
android:parentActivityName=".MainActivity"
Edit 1: You can read more here https://developer.android.com/training/implementing-navigation/ancestral.html
And the reason is when you call your "MyNotification" activity, back stack will "automatically" add it's parent activity to back stack for you, so you can press back to return to your "MainActivity"
Upvotes: 0