【问题标题】:Remove notification after cliking on Android点击安卓后删除通知
【发布时间】: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


【解决方案1】:

试试这个可能对你有帮助:

 NotificationManager  mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent = new Intent(this, Destination.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent , 0);
    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    long[] vibration = {0, 200, 50, 200};
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.app_icon)
                    .setContentTitle("MyCar Notification")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setSound(uri)
                    .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
                    .setVibrate(vibration)
                    .setAutoCancel(true)
                    .setContentText(msg);
    mBuilder.setContentIntent(contentIntent);
    int NOTIFICATION_ID = (int) System.currentTimeMillis();
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

【讨论】:

    【解决方案2】:

    notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL

    【讨论】:

      【解决方案3】:

      添加标志 Notification.FLAG_AUTO_CANCEL(或调用 .setAutoCancel(true))并重建 Unity 项目后,请转到电话设置 -> 应用程序,找到您的应用程序并单击它,然后单击“强制停止”按钮。现在再次启动您的应用程序,它应该可以工作了。

      【讨论】:

        猜你喜欢
        • 2021-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-20
        相关资源
        最近更新 更多