马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 blurred 于 2025-12-13 15:49 编辑
前言:
1.此方法由"我"提出,实现,测试
2.本方案在Ren'py-8.3.7能够实现效果,如果使用,请注意引擎版本
3.涉及到修改引擎的代码,所以请在事前把引擎目录下的rapt相关文件备份
4.使用了插件可以标注"浅唱"不标也没关系
效果预览:
压缩包:(一键解压移动文件即可食用)
应用通知.zip
(8.19 KB, 下载次数: 0)
方法思路以及部分代码解释
首先,我们知道PythonSDLActivity管理了renpy在移动系统中的运行,renpy本身并没有对通知有原生支持,所以我们自己写一个
[RenPy] 纯文本查看 复制代码 public void AndroidNotifyBox(String title, String content) {
try {
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null) {
Log.e("PythonSDLActivity", "无法获取NotificationManager");
return;
}
// 创建通知
String channelId = "renpy_channel_id";
String channelName = "Ren'Py Notifications";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
channelId,
channelName,
NotificationManager.IMPORTANCE_DEFAULT
);
channel.setDescription("Ren'Py游戏通知");
notificationManager.createNotificationChannel(channel);
}
// 图标
int iconId = getApplicationInfo().icon;
if (iconId == 0) {
iconId = android.R.drawable.ic_dialog_info;
}
// 点击返回应用
Intent intent = new Intent(this, PythonSDLActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
intent,
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ?
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT :
PendingIntent.FLAG_UPDATE_CURRENT
);
// 构建通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(iconId) // 使用应用图标
.setContentTitle(title)
.setContentText(content)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent) // 点击通知后返回应用
.setAutoCancel(true); // 点击后自动取消
int notificationId = (int) System.currentTimeMillis(); // 使用时间戳作为唯一ID
notificationManager.notify(notificationId, builder.build());
Log.i("PythonSDLActivity", "通知已发送: " + title);}
catch (Exception e) {
Log.e("PythonSDLActivity", "发送通知失败: " + e.getMessage());
e.printStackTrace();
}
} 用一个新方法AndroidNotifyBox并且使用字符串入参,并且使用NotificationsChannel构建通知,并且规定点击通知后的行为为返回游戏(详细看代码)
然后,在游戏内调用sdl的内容需要使用pyjnius模块,根据文档相关内容(安卓(Android) — Ren'Py 中文文档),我们直接导入即可,然后编写一个简单的函数调用它
[RenPy] 纯文本查看 复制代码 init python:
def AndroidNotify(title_text="Ren’py", box_text="这是一条来自Ren’py的通知", channel_id="renpy_channel"):
import jnius
if not renpy.android:
return False
try:
PythonSDLActivity = jnius.autoclass("org.renpy.android.PythonSDLActivity")
activity = PythonSDLActivity.mActivity
if activity is None:
renpy.notify("无活动")
return False
activity.AndroidNotifyBox(title_text, box_text)
return True
except Exception as e:
print(None, f"发送通知失败: {str(e)}")
return False
在游戏中使用
[RenPy] 纯文本查看 复制代码 $ AndroidNotify(title_text="测试通知", box_text="这是一条来自Ren'Py的通知")
同时,由于需要通知弹窗出现和提示,这调用了通知权限,我们需要声明这个权限并且在游戏中弹出申请权限的弹窗,这部分只要调用renpy内置的函数renpy.request_permisson()和build.android_permission申请"andriod.permission.POST_NOTIFICATION"即可
[RenPy] 纯文本查看 复制代码 $ renpy.request_permission = "andriod.permission.POST_NOTIFICATION"
define build.android_permission = ["andriod.permission.POST_NOTIFICATION"]
然后我们在app-AndroidManifest.xml中写入
[RenPy] 纯文本查看 复制代码 <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
用于声明权限,其实理论上配置了build.android_permission就可以不用写这个,但是不写会发现在应用详细中并不会出现通知权限的申请说明。
PART-2 卸载弹窗(小米特有)
说实话这个功能没啥用,但是看到renpy可以适配就顺手做了
首先我们需要打开app-string.xml声明一下字符串常量
[RenPy] 纯文本查看 复制代码 <string name="uninstall_title">这里是title</string>
<string name="uninstall_message">这里是message</string>
然后在app-AndroidManifest.xml中配置以下代码
[RenPy] 纯文本查看 复制代码 <meta-data
android:name="app_description_title"
android:resource="@string/uninstall_title" />
<meta-data
android:name="app_description_content"
android:resource="@string/uninstall_message" />
即可调用小米系统内置的接口app_description_title和app_description_content详细可见
缺点:(高情商:给后面的人优化空间 ;低情商:我太菜了,不会改 )
其实这两个功能都没啥用
|