简体   繁体   中英

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 eg 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);

}

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

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"));
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM