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

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

问题描述

现在用的解析通知我的应用程序。难道接收使用GcmBroadcastReceiver通知警报。但我看到很多通知接收到我的应用程序。我想更新通知状态栏,所以我使用的自定义接收器。当通过GcmBroadcastReceiver接到通知,我自定义的BroadcastReceiver调用。所以在我的兼具GCM和自定义通知状态栏。我只想要自定义通知。如何解决这个问题呢?

清单code:

 <接收器的Andr​​oid版本:NAME =com.parse.ParseBroadcastReceiver>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.BOOT_COMPLETED/>
            <作用机器人:名称=android.intent.action.USER_ preSENT/>
        &所述; /意图滤光器>
    < /接收器>
    <接收器
        机器人:名称=com.parse.GcmBroadcastReceiver
        机器人:权限=com.google.android.c2dm.permission.SEND>
        <意向滤光器>
            <作用机器人:名称=com.google.android.c2dm.intent.RECEIVE/>
            <作用机器人:名称=com.google.android.c2dm.intent.REGISTRATION/>

            <  - 重要提示:改变com.parse.tutorials.pushnotifications,以配合您的应用程序包名称。 - >
            <类机器人:名称=com.packagename/>
        &所述; /意图滤光器>
    < /接收器>
    <接收器
        机器人:名称=com.parse.ParsePushBroadcastReceiver
        机器人:出口=假>
        <意向滤光器>
            <作用机器人:名称=com.parse.push.intent.RECEIVE/>
            <作用机器人:名称=com.parse.push.intent.DELETE/>
            <作用机器人:名称=com.parse.push.intent.OPEN/>
        &所述; /意图滤光器>
    < /接收器>
    <接收器
        机器人:名称=com.packagename.IncomingReceiver
        机器人:启用=真
        机器人:出口=假>
        <意向滤光器>

            <作用机器人:名称=com.packagename.UPDATE_STATUS/>
        &所述; /意图滤光器>
    < /接收器>
 

应用程序类

  Parse.initialize(这一点,应用程序ID,客户端ID);
 

Incomingreceiver类:

 公共类IncomingReceiver扩展的BroadcastReceiver {
    私有静态最终诠释NOTIFICATION_ID = 1;
    公共静态INT numMessages = 0;


    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){


        尝试 {
            串动= intent.getAction();
            JSONObject的JSON =新的JSONObject(intent.getExtras()的getString(com.parse.Data));
            如果(action.equalsIgnoreCase(com.packagename.UPDATE_STATUS)){
                字符串标题=应用程序名称;

                如果(json.has(头))
                    标题= json.getString(头);

                generateNotification(背景下,标题,JSON,contenttext);
            }
        }赶上(JSONException E){
            Log.d(jsonexc,JSONException:+ e.getMessage());
        }
    }

    私人无效generateNotification(上下文的背景下,弦乐标题,JSONObject的JSON,串contenttext){
        意向意图=新的意图(背景下,NewActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,意图,0);

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

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

        mBuilder.setContentIntent(co​​ntentIntent);
        mNotifM.notify(NOTIFICATION_ID,mBuilder.build());

    }
}
 

解决方案

替换它

 <接收器
        机器人:名称=com.parse.ParsePushBroadcastReceiver
        机器人:出口=假>
        <意向滤光器>
            <作用机器人:名称=com.parse.push.intent.RECEIVE/>
            <作用机器人:名称=com.parse.push.intent.DELETE/>
            <作用机器人:名称=com.parse.push.intent.OPEN/>
        &所述; /意图滤光器>
< /接收器>
 

你的:

 <接收器
        机器人:名称=。MyReceiver
        机器人:出口=假>
        <意向滤光器>
            <作用机器人:名称=com.parse.push.intent.RECEIVE/>
            <作用机器人:名称=com.parse.push.intent.DELETE/>
            <作用机器人:名称=com.parse.push.intent.OPEN/>
        &所述; /意图滤光器>
    < /接收器>
 

和创建类:

 公共类MyReceiver扩展ParsePushBroadcastReceiver {

    保护无效onPushReceive(上下文mContext,意图意图){
    //在此输入您的自定义generateNotification();
    }

}
 

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?

Manifest code:

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

Application class:

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

Incomingreceiver class:

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

    }
} 

解决方案

Replace it

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

to your:

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

and create class:

public class MyReceiver extends ParsePushBroadcastReceiver {

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

}

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

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