单击“通知”后打开应用程序

时间:2017-02-11 15:42:45

标签: android android-notifications

我的应用中有一条通知,其中包含以下代码:

public class NewMessageNotification {

    private static final String NOTIFICATION_TAG = "NewMessage";

    public static void notify(final Context context,
                          final String exampleString,final String boday ,final int number) {
         final Resources res = context.getResources();

         // This image is used as the notification's large icon (thumbnail).
         // TODO: Remove this if your notification has no relevant thumbnail.
        final Bitmap picture = BitmapFactory.decodeResource(res, R.drawable.billsms);


        final String ticker = exampleString;
        final String title = res.getString(
            R.string.new_message_notification_title_template, exampleString);
        final String text = boday;

        final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)

            // Set appropriate defaults for the notification light, sound,
            // and vibration.


            // Set required fields, including the small icon, the
            // notification title, and text.
            .setSmallIcon(R.drawable.billsms)
            .setContentTitle(title)
            .setContentText(text)

            // All fields below this line are optional.

            // Use a default priority (recognized on devices running Android
            // 4.1 or later)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)

            // Provide a large icon, shown with the notification in the
            // notification drawer on devices running Android 3.0 or later.
            .setLargeIcon(picture)

            // Set ticker text (preview) information for this notification.
            .setTicker(ticker)

            // Show a number. This is useful when stacking notifications of
            // a single type.
            .setNumber(number)
            // If this notification relates to a past or upcoming event, you
            // should set the relevant time information using the setWhen
            // method below. If this call is omitted, the notification's
            // timestamp will by set to the time at which it was shown.
            // TODO: Call setWhen if this notification relates to a past or
            // upcoming event. The sole argument to this method should be
            // the notification timestamp in milliseconds.
            //.setWhen(...)

            // Set the pending intent to be initiated when the user touches
            // the notification.
            .setContentIntent(
                    PendingIntent.getActivity(
                            context,
                            0,
                            new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")),
                            PendingIntent.FLAG_UPDATE_CURRENT))

            // Show expanded text content on devices running Android 4.1 or
            // later.
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(text)
                    .setBigContentTitle(title)
                    .setSummaryText("Dummy summary text"))

            // Example additional actions for this notification. These will
            // only show on devices running Android 4.1 or later, so you
            // should ensure that the activity in this notification's
            // content intent provides access to the same actions in
            // another way.
            .addAction(
                    R.drawable.ic_action_stat_share,
                    res.getString(R.string.action_share),
                    PendingIntent.getActivity(
                            context,
                            0,
                            Intent.createChooser(new Intent(Intent.ACTION_SEND)
                                    .setType("text/plain")
                                    .putExtra(Intent.EXTRA_TEXT, "Dummy text"), "Dummy title"),
                            PendingIntent.FLAG_UPDATE_CURRENT))
            .addAction(
                    R.drawable.ic_action_stat_reply,
                    res.getString(R.string.action_reply),
                    null)

            // Automatically dismiss the notification when it is touched.
            .setAutoCancel(true);

        notify(context, builder.build());
    }

    @TargetApi(Build.VERSION_CODES.ECLAIR)
    private static void notify(final Context context, final Notification notification) {
        final NotificationManager nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
        nm.notify(NOTIFICATION_TAG, 0, notification);
        } else {
            nm.notify(NOTIFICATION_TAG.hashCode(), notification);
        }
    }

    /**
    * Cancels any notifications of this type previously shown using
    * .
    */
    @TargetApi(Build.VERSION_CODES.ECLAIR)
    public static void cancel(final Context context) {
        final NotificationManager nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
            nm.cancel(NOTIFICATION_TAG, 0);
        } else {
            nm.cancel(NOTIFICATION_TAG.hashCode());
        }
    }
}

我的通知触发得非常好,但我的问题是,当我点击通知中心的通知时,它无法启动我的应用。

基本上,点击我的通知后没有任何反应!谢谢。

2 个答案:

答案 0 :(得分:2)

基本上你的Pending Intent应该让你的应用程序成为活动组件之一。

Intent appIntent = new Intent(context, YourActivityName.class);
PendingIntent.getActivity(context, 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT));

同样,您可以添加其他组件,如Activity,Service和Receiver。

PendingIntent.getActivity ...
PendingIntent.getBroadcast ...
PendingIntent.getService ...

希望这有帮助。

答案 1 :(得分:0)

我找到了问题的答案。

唯一更改此代码:

 .setContentIntent(
                PendingIntent.getActivity(
                        context,
                        0,
                        new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")),
                        PendingIntent.FLAG_UPDATE_CURRENT))

到此代码:

.setContentIntent(
                    PendingIntent.getActivity(
                            context,
                            0,
                            new Intent(context.getApplicationContext(),MainActivity.class),
                            PendingIntent.FLAG_UPDATE_CURRENT))