未检测到Android清除通知

时间:2016-10-23 22:11:19

标签: android notifications

我尝试使用Firebase云消息传递推送通知并且我遇到问题,我不知道如何检测推送通知已被清除(扫描或使用清除所有按钮)。

我几乎尝试了我在Stackoverflow上看到的每一个教程都没有成功,所以我错过了一些东西。

这就是我现在所拥有的:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

final static String GROUP_KEY_EMAILS = "group_key_emails";

protected PendingIntent getContentIntent(String data)
{
    Intent notificationIntent = new Intent(getApplicationContext(), SplashScreen.class);
    notificationIntent.putExtra("custom", data);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    return PendingIntent.getActivity(getApplicationContext(), 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}

protected PendingIntent getDeleteIntent()
{
    Intent intent = new Intent(getApplicationContext(), NotificationBroadcastReceiver.class);
    intent.setAction("notification_cancelled");
    return PendingIntent.getBroadcast(getApplicationContext(), 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    if(remoteMessage.getNotification() != null)
    {
        try {
            Map<String, String> data = remoteMessage.getData();
            String customData = data.get("custom");
            JSONObject customJSON = new JSONObject(customData);
            final JSONObject pushData = customJSON.getJSONObject("custom data");

            String message = remoteMessage.getNotification().getBody();
            Notification notif1 = new android.support.v4.app.NotificationCompat.Builder(getApplicationContext())
                    .setContentTitle("My app")
                    .setContentText(message)
                    .setSmallIcon(R.drawable.icon)
                    .setAutoCancel(true)
                    .setGroup(GROUP_KEY_EMAILS)
                    .setContentIntent(getContentIntent(customData))
                    .setDeleteIntent(getDeleteIntent())
                    .build();

            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
            notificationManager.notify(1, notif1);
            AuxiliaryFunctions.addPushNotificationsMessage(getApplicationContext(), message);
            int notifId = AuxiliaryFunctions.readPushNotificationsMessagesNum(getApplicationContext());
            if(notifId > 1)
            {
                NotificationCompat.InboxStyle style = new android.support.v4.app.NotificationCompat.InboxStyle()
                        .setSummaryText(notifId + " new messages");
                JSONArray messages = AuxiliaryFunctions.readPushNotificationsMessages(getApplicationContext());
                for(int i = 0; i < messages.length(); i++)
                {
                    String localmessage = messages.getString(i);
                    style.addLine(localmessage);
                }
                Notification summaryNotification = new android.support.v4.app.NotificationCompat.Builder(getApplicationContext())
                        .setContentTitle("My app "+notifId+" new messages")
                        .setSmallIcon(R.drawable.iconplus)
                        .setStyle(style)
                        .setGroup(GROUP_KEY_EMAILS)
                        .setGroupSummary(true)
                        .setAutoCancel(true)
                        .setContentIntent(getContentIntent(""))
                        .setDeleteIntent(getDeleteIntent())
                        .build();

                notificationManager.notify(1, summaryNotification);
            }

        } catch (JSONException e) {

        }
    }
  }
}

这是我的通知广播接收器:

public class NotificationBroadcastReceiver  extends BroadcastReceiver   
{

  @Override
  public void onReceive(Context context, Intent intent)
  {
    Log.e("", "NotificationBroadcastReceiver:onReceive");
  }
}

最后我在清单中声明了这一点:

       <receiver android:name="auxiliary.NotificationBroadcastReceiver">
        <intent-filter>
            <action android:name="notification_cancelled"/>
        </intent-filter>
    </receiver>

嗯,通知显示正确,如果有多个通知,并且如果用户点击通知(摘要与否),一切进展顺利,通知处理没有问题。 但是,如果用户在通知中进行扫描(摘要与否)或单击“清除所有通知”按钮,则通知会消失,但永远不会调用NotificationBroadcastReceiver(日志NotificationBroadcastReceiver:onReceive未显示在控制台中)。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我非常惭愧!我发现了问题的原因,这是完全愚蠢的。

在我的AndroidManifest中,我有这个:

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

<h1 class="title">TEST </h1>
<p id="test">  <p>

我在辅助之前忘了点。一旦我说出来:

<receiver android:name="auxiliary.NotificationBroadcastReceiver">
        <intent-filter>
            <action android:name="notification_cancelled"/>
        </intent-filter>
    </receiver>

调用接收器回调。

让我感到惊讶的是,AndroidManifest没有错误解决错误的路径。