So here’s the deal.
I created a user defined class. It contains a method which returns a notification object. Now I want this method to be a little bit flexible. Like pass the activity which will open when the user clicks the notification in the notification bar. Here’s the method
public Notification getUserNotificationObject(String status, String message, String tickerText, boolean isOngoingEvent){
Notification notification = new Notification(R.drawable.image, tickerText, System.currentTimeMillis());
long vibInterval = (long) context.getResources().getInteger(R.integer.vibrateInterval);
notification.vibrate = new long[] {vibInterval, vibInterval, vibInterval, vibInterval, vibInterval};
Intent notifyIntent = new Intent(context, HomeScreen.class);
CharSequence contentTitle = "Title";
CharSequence contentText = status + "-" + message;
notification.setLatestEventInfo(context, contentTitle, contentText, PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_CANCEL_CURRENT));
notification.ledARGB = Color.argb(100, 0, 254, 0);
notification.ledOnMS = 500;
notification.ledOffMS = 500;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
if(isOngoingEvent){
notification.flags |= Notification.FLAG_ONGOING_EVENT;
}
return notification;
}
I want to be able to pass the activity as a parameter in this instead of the
HomeScreen.class
used above in intent definition (for giving additional control to the user of this class (or other developers) to select which activity to open when notification is clicked). I tried using Activity as one of the parameters for this method, but whenever I tried passing another activity while calling this method like “Activity2” or “Activity2.this” it gives me error saying:
No enclosing instance of the type Activity2 is accessible in scope
Is there any work-around for this or any way to pass the activity as a parameter. Or should I just differentiate those based on NotificationID.
Any help in this regard or any correction in the code above is welcome. (“context” is a class level variable so don’t worry about that. This piece of code is working fine).
The type of
HomeScreen.classisClass. So you could pass an instance of aClassto indicate the next activity. For example (formatted for readability):and call with:
More flexible, though, might be to pass an
Intentto your function. That way, the caller can create theIntentin the way they want, and would allow them to add additional data to the intent if they need to. Creating anew Intentinside your function does not permit that flexibility.and call with: