dominic
dominic

Reputation: 318

Android : Click notification to launch activity

How can one differentiate the Notifications and pass them to different activities? In the below method the third parameter(String tag) value is retreived from onMessage() that contains a value. According to that value i wanted to differentiate which URL to open e.g Google or Facebook. The app terminates after i include the intent code in the "if" conditions below. Please Help

    private static void generateNotification(Context context, String message,
        String tag) {
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher,
            message, when);
    String title = context.getString(R.string.app_name);

    Log.d("GCM", "Tag Value " + tag);

        if (tag.equals("one")) {
            // pass to a different activity
                       notificationIntent = new Intent(Intent.ACTION_VIEW,
                Uri.parse("https://www.google.com"));

        }

        if (tag.equals("two")) {
            // pass to a different activity
                    notificationIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("https://www.facebook.com"));
        }


    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);

}

Upvotes: 1

Views: 2854

Answers (2)

Parvin Gasimzade
Parvin Gasimzade

Reputation: 26032

Change your code to something like :

  if (tag.equals("one")) {
     notificationIntent = new Intent(Intent.ACTION_VIEW,
            Uri.parse("https://www.google.com"));
  } else if (tag.equals("two")) {
     notificationIntent = new Intent(Intent.ACTION_VIEW,
            Uri.parse("https://www.facebook.com"));
  }

Upvotes: 0

JULI
JULI

Reputation: 126

On Manifest create intent filter for activities,that you need launch like this

<activity
        android:name=".activities.TabsActivity"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="com.dittoadvertising.push" >
            </action>
            <category android:name="android.intent.category.DEFAULT" >
            </category>
        </intent-filter>

when Uri.parse("https://www.facebook.com") = action name in filter

and in your activities will be called method onNewIntent(Intent newIntent), then intent incoming

Upvotes: 1

Related Questions