i tried this crate but this's old and stuck my app after notification shown.
i written this code by ChatGPT , it's not clean and maybe some wrongs here.
here's rust core code
#[cfg(target_os="macos")]
pub fn mac_show_notification(app: tauri::AppHandle, message :NotifyMessage) -> Result<()> {
use objc2::runtime::Object;
use objc2_user_notifications::{UNAuthorizationOptions, UNNotificationAction};
let center = unsafe { UNUserNotificationCenter::currentNotificationCenter() };
unsafe {
center.getNotificationSettingsWithCompletionHandler(&RcBlock::new(|settings: NonNull<UNNotificationSettings>| {
let settings: Id<UNNotificationSettings> = Id::retain(settings.as_ptr() as *mut NSObject).unwrap().downcast().unwrap();
let authorization_status = settings.authorizationStatus();
let alert_style = settings.alertStyle();
let sound_setting = settings.soundSetting();
let badge_setting = settings.badgeSetting();
println!("通知授权状态:{:?}", authorization_status);
println!("通知提醒样式:{:?}", alert_style);
println!("声音设置:{:?}", sound_setting);
println!("标记设置:{:?}", badge_setting);
}));
}
// 请求通知权限
unsafe {
let handler = RcBlock::new(|grated: Bool, err: *mut NSError| {
if grated == Bool::NO {
let err_obj = Id::retain(err as *mut NSObject).unwrap();
let err_id: Id<NSError> = err_obj.downcast().unwrap();
println!("报错: {}", err_id.localizedDescription().to_string());
println!("domain:{}", err_id.domain().to_string());
println!("code:{}", err_id.code().to_string());
println!("权限申请失败");
}
});
center.requestAuthorizationWithOptions_completionHandler(
UNAuthorizationOptions::Sound | UNAuthorizationOptions::Alert,
&handler
);
}
let content = unsafe {
let content = UNMutableNotificationContent::new();
content.setTitle(&NSString::from_str(&message.title));
content.setBody(&NSString::from_str(&message.body));
content.setSound(Some(&UNNotificationSound::defaultSound()));
let action = UNNotificationAction::actionWithIdentifier_title_options_icon(
&NSString::from_str("show_detail"),
&NSString::from_str("查看"),
UNNotificationActionOptions::Foreground,
None,
);
let category = UNNotificationCategory::categoryWithIdentifier_actions_intentIdentifiers_options(
&NSString::from_str("mixin-category"),
&NSArray::from_retained_slice(&[action]),
&NSArray::array(),
objc2_user_notifications::UNNotificationCategoryOptions::empty(),
);
unsafe {
center.setNotificationCategories(&NSSet::from_retained_slice(&[category]));
}
content.setCategoryIdentifier(&NSString::from_str("mixin-category"));
content
};
let request = unsafe {
UNNotificationRequest::requestWithIdentifier_content_trigger(
&NSString::from_str("notification"),
&content,
None,
)
};
unsafe {
center.addNotificationRequest_withCompletionHandler(&request, None);
}
Ok(())
}
and Info.plist content under src-tauri
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" ".0.dtd">
<plist version="1.0">
<dict>
<key>NSUserNotificationUsageDescription</key>
<string>需要开启消息通知。</string>
</dict>
</plist>
but error code keep saying:
报错: The operation couldn’t be completed. (UNErrorDomain error 1.)
domain:UNErrorDomain
code:1
and i don't see my app name under system Notification.
Did I miss any configuration steps? Or is there a flaw in the code?