【问题标题】:Custom Push Notification coming twice using Parse.com and Android使用 Parse.com 和 Android 的自定义推送通知两次
【发布时间】:2016-05-09 09:00:55
【问题描述】:

我正在使用 parse.com android 处理自定义推送通知。 Push 已成功集成到我的应用程序中,但问题是我一次收到两个通知。

一个来自我的自定义接收器的图像和另一个来自操作系统的默认通知,没有任何图像,并且该通知甚至没有从通知栏中删除,如果我要删除它,它会一次又一次地出现在通知托盘上。我还将在下面粘贴我的代码 sn-p 和图像。

// My Custom Receiver Class

public class CustomPushReceiver extends ParsePushBroadcastReceiver {
    private final String TAG = CustomPushReceiver.class.getSimpleName();

    private NotificationUtils notificationUtils;

    private Intent parseIntent;

    public CustomPushReceiver() {
        super();
    }

    @Override
     public void onReceive(Context context, Intent intent) {
        super.onPushReceive(context, intent);

        if (intent == null)
            return;
            parseIntent = intent;
        try {
            String action = intent.getAction();
            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
            if (action.equalsIgnoreCase("com.parse.push.intent.RECEIVE")) {
//                NOTIFICATION_ID++;
                String title = "DW";
                if (json.has("alert"))
                {
                    String text = json.getString("alert");
                    generateNotification(context, title, json, text,parseIntent);
                }
            }
        } catch (JSONException e) {
        }
 }

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

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

   private void generateNotification(Context context, String title, JSONObject json, String text, Intent intent) {
//        Intent intent = new Intent(context, home.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        android.support.v4.app.NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.log_pic_small)
                        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.log_pic))
                        .setAutoCancel(true)
                        .setContentTitle(title)
                        .setContentText(text)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(text))
                        .setTicker(text)
                        .setLights(Color.GREEN, 500, 500);
        mNotifM.cancel(0);
        mBuilder.setContentIntent(contentIntent);
        mNotifM.notify(110, mBuilder.build());
    }
}

清单文件

<service android:name="com.parse.PushService" />

        <receiver
            android:name="com.restaurant.services.CustomPushReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="com.parse.push.intent.RECEIVE" />
                <action android:name="com.parse.push.intent.DELETE" />
                <action android:name="com.parse.push.intent.OPEN" />
            </intent-filter>
        </receiver>
        <receiver
            android:name="com.parse.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <!-- IMPORTANT: Change "info.androidhive.parsenotifications" to match your app's package name. -->
                <category android:name="com.dw" />
            </intent-filter>
        </receiver>

【问题讨论】:

  • 确认服务器是否发送了两次通知。
  • 服务器只发送一次推送。 @MuhammadBabar

标签: android parse-platform push-notification notifications


【解决方案1】:

不要每次都生成通知,而是在应用程序启动时第一次初始化

CustomPushReceiver customReciever = new CustomPushReceiver (getApplicationContext());

//将此接收器设置为您的推送工厂

getPushManager().setNotificationFactory(customReciever);

【讨论】:

    【解决方案2】:

    我认为您的问题是由您的public void onReceive(Context context, Intent intent) 中的super.onPushReceive(context, intent); 方法产生的。

    我认为您应该覆盖protected void onPushReceive(Context context,Intent intent) 并在那里处理推送通知。 注意:如果您覆盖 onPushReceive,请不要调用 super,因为它会再次显示推送。

    根据 Parse 文档:

    收到推送通知时调用。默认情况下,广播 如果数据中存在“动作”并且 如果“警报”和“标题”出现在 数据。

    编辑

    公共类 CustomPushReceiver 扩展 ParsePushBroadcastReceiver { .....这里的初始化代码来自你的例子......

    @Override
    public void onReceive(Context context, Intent intent) {
      super.onReceive(Context context, Intent intent);
    }
    @Override
    public void onPushReceive(Context context, Intent intent) {
        //NO SUPER CALL
      ....exact code from your onReceive.....
    }
    ... rest of the code from your example...
    

    }

    我想你可以从这里处理它。

    【讨论】:

    • 感谢您在这里的回答。你能在这里放一个小代码sn-p,这样我就很容易理解了。我现在有点理解它。
    • 非常感谢。它解决了我的问题。你是对的,我为 onPushReceive 调用 super 是一个错误,它一次又一次地调用它。这就是为什么另一个通知也来了。
    • @danypata。你能帮助我吗?请
    • @jojo 我能为您做什么?这个答案太老了:)
    • @danypata。我有一个这样的问题。非常感谢:stackoverflow.com/q/52235660/10211127
    【解决方案3】:

    检查您是否在 Manifest 文件中添加了两个接收器,一个用于默认解析接收器,另一个用于您的自定义接收器。如果是这样,请删除解析默认接收器并仅保留您的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      相关资源
      最近更新 更多