Tuesday, 19 February 2019

How to navigate to the specific class on click of notification in android. | For the same add home screen in backstack

MainActivity.class:- call below method on the click of any button :-
private void addNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("My Notification")
            .setContentText("This is my first notification app.My notification supriya");
    Intent notificationIntent = new Intent(this, MyNotificationDetail.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MyNotificationDetail.class);
    stackBuilder.addNextIntent(notificationIntent);
    PendingIntent pendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, builder.build());

}

NotificationDetail.class:-
public class MyNotificationDetail extends AppCompatActivity {
    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification);
    }
}
notification.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:orientation="vertical"    
android:layout_width="fill_parent"    
android:layout_height="fill_parent" >

    <TextView        
android:layout_width="fill_parent"        
android:layout_height="400dp"        
android:text="Hi, Your Detailed notification view goes here...." />
</LinearLayout>

Manifests.xml:-
<activity    
android:name=".MyNotificationDetail"    
android:label="Detail 0f Notification"    
android:parentActivityName=".MainActivity">
    <meta-data        
android:name="android.support.PARENT_ACTIVITY"        
android:value=".MainActivity" />

</activity>

ScreenShots :-
  

No comments:

Post a Comment