android - 使用自定义 broadcastreceiver 接收 Parse 通知

标签 android parse-platform android-broadcastreceiver

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

list 代码:

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

Incomingreceiver 类:

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

}

关于android - 使用自定义 broadcastreceiver 接收 Parse 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26859616/

相关文章:

android - 我如何知道android gradle插件的applicationVariants中的属性?

java - 尝试与 Android Intent 共享图像

javascript - Parse.com 在保存之前将用户指针添加到类中

android - 当我的应用程序处于后台时,如何检测网络状态/wifi 状态已更改?

android - BroadcastReceiver.getResultsExtra() 与 Intent.getExtras()

Android广播接收器自定义权限

java - 与棉花糖相比,牛轧糖要慢得多

android Notification - 如何不在前台显示

java - 当另一个声音在 android studio 中播放时停止播放声音

javascript - 尝试写入用户类解析,masterkey 不工作