0

this below code is my simple NotificationBar class and i can use that in all of project. i want to set Activity as an Intent for StartActivity from that. how to update my class to have this functionality?

public class TsmsNotification /* extends Activity*/ {

    private Context context;
    public TsmsNotification( Context context ) {
        this.context = context;
    }

    public void SetNotifyText( String title , String text){
        Notification notification = new Notification(
                R.drawable.tsms_icon,
                title, //Text
                System.currentTimeMillis() //When to display - i.e. now
        );
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        PendingIntent pi = PendingIntent.getActivity(this.context, 0, intent, 0);

        //Add to the Notification
        notification.setLatestEventInfo(
                this.context,
                title, //Title of detail view
                text , pi
        );

        //Display the Notification
        NotificationManager nm = (NotificationManager)this.context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(0, notification);

        /*intent.setClass(TsmsNotification.this, ReceivedSMSFragment.class);*/
    }
}

i can use this class as:

new TsmsNotification(context)
                .SetNotifyText(
                        context.getString(R.string.notify_text),
                        context.getString(R.string.total_inbox_sms)
                );

1 Answers1

0

usually You start a Activity from a notification like this:

       Intent startActivityIntent = new Intent(context, YourActivity.class);
       PendingIntent pi= PendingIntent.getActivity(context,
            0, startActivityIntent ,
            PendingIntent.FLAG_CANCEL_CURRENT);

       notification.setLatestEventInfo(
            this.context,
            title, //Title of detail view
            text , pi
    );

    //Display the Notification
    NotificationManager nm = (NotificationManager)this.context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(0, notification);

By clicking on the notification, the activity should start.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49