如何创建带可控按钮的Outlook自定义通知?求指导
自定义双按钮通知实现方案
Android 平台实现步骤
- 创建自定义通知布局
在res/layout下创建XML布局文件(比如custom_notification.xml),包含通知标题、内容区域和两个按钮:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/notification_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold"/> <TextView android:id="@+id/notification_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" android:layout_marginTop="8dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="16dp"> <Button android:id="@+id/btn_action1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="按钮1"/> <Button android:id="@+id/btn_action2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="按钮2" android:layout_marginStart="8dp"/> </LinearLayout> </LinearLayout>
- 构建并显示通知
使用RemoteViews加载自定义布局,为每个按钮绑定PendingIntent,通过Intent的额外参数区分点击事件:
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager // 适配Android 8.0及以上的通知渠道 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel("custom_notif_channel", "自定义通知", NotificationManager.IMPORTANCE_DEFAULT) notificationManager.createNotificationChannel(channel) } val remoteViews = RemoteViews(packageName, R.layout.custom_notification) remoteViews.setTextViewText(R.id.notification_title, "自定义通知标题") remoteViews.setTextViewText(R.id.notification_content, "这是自定义通知的内容") // 按钮1的点击事件 val intent1 = Intent(this, NotificationActionReceiver::class.java).apply { putExtra("ACTION_TYPE", "ACTION_1") } val pendingIntent1 = PendingIntent.getBroadcast(this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) remoteViews.setOnClickPendingIntent(R.id.btn_action1, pendingIntent1) // 按钮2的点击事件 val intent2 = Intent(this, NotificationActionReceiver::class.java).apply { putExtra("ACTION_TYPE", "ACTION_2") } val pendingIntent2 = PendingIntent.getBroadcast(this, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) remoteViews.setOnClickPendingIntent(R.id.btn_action2, pendingIntent2) val notification = NotificationCompat.Builder(this, "custom_notif_channel") .setSmallIcon(R.drawable.ic_notification) .setCustomContentView(remoteViews) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .build() notificationManager.notify(1, notification)
- 处理按钮点击事件
创建广播接收器NotificationActionReceiver,根据Intent参数执行对应逻辑:
class NotificationActionReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { when (intent?.getStringExtra("ACTION_TYPE")) { "ACTION_1" -> { // 执行按钮1的逻辑,比如打开页面、发送请求等 Toast.makeText(context, "按钮1被点击", Toast.LENGTH_SHORT).show() } "ACTION_2" -> { // 执行按钮2的逻辑 Toast.makeText(context, "按钮2被点击", Toast.LENGTH_SHORT).show() } } } }
记得在AndroidManifest.xml中注册接收器:
<receiver android:name=".NotificationActionReceiver" />
iOS 平台实现步骤
- 注册通知类别与权限
在AppDelegate或启动代码中,先请求通知权限,并注册包含两个按钮动作的通知类别:
import UserNotifications func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // 请求通知权限 UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in if granted { DispatchQueue.main.async { application.registerForRemoteNotifications() } } } // 创建两个通知动作 let action1 = UNNotificationAction(identifier: "ACTION_1", title: "按钮1", options: []) let action2 = UNNotificationAction(identifier: "ACTION_2", title: "按钮2", options: []) // 创建通知类别 let category = UNNotificationCategory(identifier: "CUSTOM_NOTIF_CATEGORY", actions: [action1, action2], intentIdentifiers: [], options: []) // 注册类别 UNUserNotificationCenter.current().setNotificationCategories([category]) // 设置代理处理点击事件 UNUserNotificationCenter.current().delegate = self return true }
- 发送自定义通知
创建通知内容并关联已注册的类别,发送本地通知(远程通知逻辑类似,只需调整推送 payload):
func sendCustomNotification() { let content = UNMutableNotificationContent() content.title = "自定义通知标题" content.body = "这是自定义通知的内容" content.categoryIdentifier = "CUSTOM_NOTIF_CATEGORY" // 关联类别 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) let request = UNNotificationRequest(identifier: "custom_notif_request", content: content, trigger: trigger) UNUserNotificationCenter.current().add(request) { error in if let error = error { print("通知发送失败: \(error.localizedDescription)") } } }
- 处理按钮点击事件
实现UNUserNotificationCenterDelegate的方法,根据动作标识符区分逻辑:
extension AppDelegate: UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { switch response.actionIdentifier { case "ACTION_1": // 执行按钮1的逻辑 print("按钮1被点击") case "ACTION_2": // 执行按钮2的逻辑 print("按钮2被点击") default: break } completionHandler() } }
内容的提问来源于stack exchange,提问作者R. Mohammad
相关产品推荐
相关产品推荐

