使用自定义广播接收器接收解析通知 [英] Receive Parse notification using custom broadcastreceiver

查看:31
本文介绍了使用自定义广播接收器接收解析通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用解析通知.我正在使用 GcmBroadcastReceiver 接收通知警报.但是我看到我的应用程序收到了很多通知.我想更新状态栏中的通知,所以我使用了自定义接收器.当通过 GcmBroadcastReceiver 接收通知时,我的自定义广播接收器调用.所以在我的状态栏中同时有 gcm 和自定义通知.我只想要自定义通知.如何解决这个问题?

Am using parse notification in my app. Am receiving notification alert using GcmBroadcastReceiver. But am seeing lot of notification receiving to my app. I thought to update notification in status bar so i used custom receiver. When receiving notification through GcmBroadcastReceiver, my custom broadcastreceiver called. So in my status bar having both gcm and custom notifications. I want custom notification only. How to solve this problem?

清单代码:

    <receiver android:name="com.parse.ParseBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
        </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 "com.parse.tutorials.pushnotifications" to match your app's package name. -->
            <category android:name="com.packagename" />
        </intent-filter>
    </receiver>
    <receiver
        android:name="com.parse.ParsePushBroadcastReceiver"
        android:exported="false" >
        <intent-filter>
            <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.packagename.IncomingReceiver"
        android:enabled="true"
        android:exported="false" >
        <intent-filter>

            <action android:name="com.packagename.UPDATE_STATUS" />
        </intent-filter>
    </receiver>

应用类:

Parse.initialize(this, "app id", "client id");

传入接收器类:

 public class IncomingReceiver extends BroadcastReceiver {
    private static final int NOTIFICATION_ID = 1;
    public static int numMessages = 0;


    @Override
    public void onReceive(Context context, Intent intent) {


        try {
            String action = intent.getAction();
            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
            if (action.equalsIgnoreCase("com.packagename.UPDATE_STATUS")) {
                String title = "appname";

                if (json.has("header"))
                    title = json.getString("header");

                generateNotification(context, title, json,contenttext);
            }
        } catch (JSONException e) {
            Log.d("jsonexc", "JSONException: " + e.getMessage());
        }
    }

    private void generateNotification(Context context, String title, JSONObject json, String contenttext) {
        Intent intent = new Intent(context, NewActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);

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

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.app_icon).setContentTitle(title).setContentText("contenttext").setNumber(++numMessages);

        mBuilder.setContentIntent(contentIntent);
        mNotifM.notify(NOTIFICATION_ID, mBuilder.build());

    }
} 

推荐答案

替换

<receiver
        android:name="com.parse.ParsePushBroadcastReceiver"
        android:exported="false" >
        <intent-filter>
            <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=".MyReceiver"
        android:exported="false" >
        <intent-filter>
            <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>

并创建类:

public class MyReceiver extends ParsePushBroadcastReceiver {

    protected void onPushReceive(Context mContext, Intent intent) {
    //enter your custom here generateNotification();
    }

}

这篇关于使用自定义广播接收器接收解析通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆