为通知创建 PendingIntent 而不指定 activity

Create PendingIntent for notification without specifying activity

有没有办法为通知创建 PendingIntent 或显示来自 "data" 层的通知。

在那一层我没有activityclass.

那么 Intent 是否可以自动选择一个标记为 "default" 或 "launcher" 的 activity?

        <activity
            android:name=".feature.splash.SplashActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

您可以使用 PendingIntent.getBroadcast() :

 Intent broadcastIntent = new Intent(mContext, NotificationReceiver.class);
 broadcastIntent.putExtra(Const.NOTIFICATION_ID,"1";
 PendingIntent cancleIntent = PendingIntent.getBroadcast(mContext,
            0, broadcastIntent, PendingIntent.FLAG_CANCEL_CURRENT);

public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.hasExtra(Const.NOTIFICATION_ID)) {
        int notificationId = intent.getIntExtra(Const.NOTIFICATION_ID, 0);

       // do your logic like cancle notification
         NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            manager.cancel(notificationId);

    }


}
}