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

标签 android parse-platform push-notification notifications

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

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

// 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());
    }
}

list 文件

<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

最佳答案

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

CustomPushReceiver customReciever = new CustomPushReceiver (getApplicationContext());

//将此 RECIEVER 设置为您的推送工厂

getPushManager().setNotificationFactory(customReciever);

关于android - 使用 Parse.com 和 Android 两次自定义推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37111608/

相关文章:

android - 如何支持 IBM Worklight 6.2 for Android 的客户端证书相互认证?

java - 无法解析配置 ':pushwoosh-react-native-plugin:classpath' 的所有 Artifact

facebook-graph-api - 哪个 Unity 平台最适合 Facebook Canvas 应用程序

swift - 在 Swift 中使用 Parse 获取 Twitter 个人资料图片

ios - 我是否需要 iOS 通知服务扩展的分发配置文件?

黑莓上的安卓 : How to Play Videos

android - 使用 URL 从 Firebase 存储中删除文件

ios - 如何从 Swift 的 Parse 后端为用户提供国家/地区下拉列表?

ios - 休斯顿 apn 推送不执行任何操作(无错误)

ios - 推送通知 : I almost always get "Connection failed" but it worked few times