其他分享
首页 > 其他分享> > 自定义推送通知使用Parse.com和Android两次出现

自定义推送通知使用Parse.com和Android两次出现

作者:互联网

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

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

// 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>

enter image description here

解决方法:

我认为您的问题是由方法super.onPushReceive(context,intent);生成的.在您的公共void onReceive(Context context,Intent intent)中.

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

根据解析文档:

Called when the push notification is received. By default, a broadcast
intent will be sent if an “action” is present in the data and a
notification will be show if “alert” and “title” are present in the
data.

编辑

公共类CustomPushReceiver扩展了ParsePushBroadcastReceiver {
    ….. initple的初始化代码….

@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...

}

我想您可以从这里处理.

标签:parse-platform,push-notification,notifications,android
来源: https://codeday.me/bug/20191118/2030070.html