【发布时间】:2015-07-27 17:31:07
【问题描述】:
我目前正在使用 Unity 插件,它允许我推送“本地”通知。它工作正常,除了一件事:点击后通知不会消失。
代码如下:
public void onReceive(Context context, Intent intent)
{
Log.d("Unity", "Alarm Recieved!");
NotificationManager mNM = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Bundle bb = intent.getExtras();
Class<?> cc = null;
try
{
cc = context.getClassLoader().loadClass("com.unity3d.player.UnityPlayerProxyActivity");
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
return;
}
final PackageManager pm = context.getPackageManager();
ApplicationInfo applicationInfo = null;
try
{
applicationInfo = pm.getApplicationInfo(context.getPackageName(),PackageManager.GET_META_DATA);
}
catch (NameNotFoundException e)
{
e.printStackTrace();
return;
}
final int appIconResId = applicationInfo.icon;
Notification notification = new Notification(appIconResId, (String)bb.get("name"), System.currentTimeMillis());
//notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL
int id = (int)(Math.random()*10000.0f)+1;
PendingIntent contentIntent = PendingIntent.getActivity(context, id, new Intent(context, cc), 0);
notification.setLatestEventInfo(context, (String)bb.get("title"), (String)bb.get("label"), contentIntent);
Log.i("Unity", "notify("+id+") with "+(String)bb.get("title")+", "+(String)bb.get("label"));
mNM.notify(id, notification);
}
即使我不是 java 开发人员,我也能理解这段代码的大部分内容。我已经尝试了一些技巧,例如:
notification.setAutoCancel(true);
notification.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
但正如我所料,这行不通......
那么,有人可以向我解释如何实现这一目标吗? 谢谢小伙伴们!
【问题讨论】:
-
你试过了吗:new Notification.Builder().setAutoCancel(true).build()?
-
还没有。我应该将它复制/粘贴到方法的底部吗?
-
不要使用“new Notification()”,而是使用构建器创建通知。它被称为构建器模式。
标签: java android unity3d notifications