Is setContentIntent(PendingIntent) required in NotificationCompat.Builder?

前端 未结 1 1210
失恋的感觉
失恋的感觉 2020-12-11 02:35

Calling :

public static void triggerTestNotification(Context ctx, String tag, int id) {
    Notification not = new NotificationCompat.Builder(ctx)
        .s         


        
1条回答
  •  有刺的猬
    2020-12-11 03:21

    If you use a caching service like waybackmachine and you look for previous versions of the Notifications guide, you will see that the guide does tell you that the contentIntent is required.

    This is reflected in the Android source as well. NotificationManagerService handles the checking of Notifications before displaying them.

    In Gingerbread, as part of the enqueueNotificationInternal() method, it has this check:

    if (notification.icon != 0) {
        if (notification.contentView == null) {
              throw new IllegalArgumentException("contentView required: pkg=" + pkg
                        + " id=" + id + " notification=" + notification);
        }
        if (notification.contentIntent == null) {
            throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
                    + " id=" + id + " notification=" + notification);
        }
    }
    

    On later Android versions, such as Ice Cream Sandwich, that check is gone:

    if (notification.icon != 0) {
        if (notification.contentView == null) {
           throw new IllegalArgumentException("contentView required: pkg=" + pkg
                  + " id=" + id + " notification=" + notification);
        }
    }
    

    Thus, a contentIntent is required on Gingerbread and below.

    0 讨论(0)
提交回复
热议问题