【问题标题】:parse.com - Delete specific notification from status bar on androidparse.com - 从 android 的状态栏中删除特定通知
【发布时间】:2015-09-01 10:51:19
【问题描述】:

我想使用以下函数从 android 状态栏中删除特定通知:cancel()。

为此,我需要通知 ID。

如何从 parse.com 通知中获取此 ID?

【问题讨论】:

  • 您能先发布您的自定义 BroadCastReceiverClass 的代码吗?
  • 我不使用 BroadCastReceiverClass。
  • 在 BroadCastRecieverClass 中设置了通知 ID。现在默认情况下,解析在您的应用程序中实现自己的类,但要控制通知的 id、intent 和其他详细信息,您必须先设置它。
  • 我不使用 BroadCastReceiverClass。我使用:ParsePushBroadcastReceiver。
  • 抱歉,我的网络中断了。下面的答案不完整,但足以让您抢先一步。我用我的手机写。您在使用以下代码时遇到任何特殊问题吗?

标签: android parse-platform push


【解决方案1】:

好的,所以这需要一些实验。基本上你需要一个 BroadCaster Parse 的自定义类。因此,您需要扩展它并创建一个您自己的自定义类,您可以在其中处理自定义事件,例如使用通知生成器制作通知

编辑:替换了整个代码。这是有效的。

  public class MyCustomReceiver extends ParsePushBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        String payload=intent.getExtras().toString();

        Log.d("PARSEPUSH", payload);
        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent piIntent=PendingIntent.getActivity(context,0,
                notificationIntent,0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setContentTitle("My Notification").setSmallIcon(android.R.drawable.star_on)
                .setContentText("Message Body").setAutoCancel(true);
        mBuilder.setContentIntent(piIntent);

        Notification pnNotif = mBuilder.build();

        Integer notificationId=0;

        NotificationManager mNotificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(notificationId, pnNotif);  // Set notification ID

    }

    @Override
    protected void onPushReceive(Context context, Intent intent) {
        super.onPushReceive(context, intent);
    }

    public MyCustomReceiver() {
        super();
    }

    @Override
    protected Notification getNotification(Context context, Intent intent) {
    return null;
    }
}

确保更新清单并将 ParsePushBroadcastReveiver 替换为 MyCustomReceiver。

【讨论】:

  • 很抱歉,一行没有被复制。我忘了提及通知使用哪个图标。它现在来了.. 使用 intent.getExtras.toString() 来读取推送通知中发送的有效负载。将其视为 json 并从中检索消息负载。
  • 我尝试了这段代码,但是当我这样做时,我没有在状态栏收到通知。我在哪里设置ID?例如我需要这样的东西:- notificationManager.notify(MY_NOTIFICATION_ID, notification);
  • 通知出现在状态栏。这对我来说很好。检查两件事。确保您看到的是 Log.d 输出。这至少会表明正在调用该广播公司。其次,确保您的清单已正确声明和更新。见最后一行 mNotificationManager.notify(notificationId, pnNotif); . notificationId 是您的自定义 ID。只需将其替换为 MY_NOTIFICION_ID 即可。请记住,这只能是整数值。
猜你喜欢
  • 2011-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多