简体   繁体   English

在自定义BroadcastReceiver中未在Android上接收解析推送通知

[英]Not Receiving Parse Push Notifications on Android in Custom BroadcastReceiver

I can successfully send a push notification using Parse with the following data, but can't receive the message in my custom Broadcast Receiver. 我可以使用Parse成功发送带有以下数据的推送通知,但无法在我的自定义广播接收器中接收消息。 Following the Parse Android notification guide: https://parse.com/docs/push_guide#receiving/Android . 遵循Parse Android通知指南: https//parse.com/docs/push_guide#receiving/Android Any help would be appreciated! 任何帮助,将不胜感激!

Sending the push: 发送推送:

ParseQuery<ParseInstallation> userQuery = ParseInstallation.getQuery();
                userQuery.whereContainedIn("user", arg0);

                JSONObject data= null;
                try {
                    data = new JSONObject("{\"title\" : \"Hush!\"," +
                                            "\"intent\" : \"ChatWindowActivity\"," +
                                            "\"action\" : \"com.hush.UPDATE_STATUS\"," +
                                            "\"chatId\" :" + getObjectId() + "}");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                ParsePush push = new ParsePush();
                push.setQuery(userQuery);
                push.setData(data);
                push.setMessage("One of your friends wants to chat...");
                push.sendInBackground();

AndroidManifest settings: AndroidManifest设置:

<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.hush.HushPushReceiver" android:exported="false">
    <intent-filter>
        <action android:name="com.hush.UPDATE_STATUS" />
    </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" />

        <category android:name="com.hush" />
    </intent-filter>
</receiver>

Custom Push Receiver: 自定义推送接收器:

package com.hush;
import java.util.Iterator;

import org.json.JSONException; 
import org.json.JSONObject;

import android.content.BroadcastReceiver; import android.content.Context; 
import     android.content.Intent; import android.util.Log; import android.widget.Toast;

import com.parse.ParseAnalytics;

public class HushPushReceiver extends BroadcastReceiver {

private static final String TAG = "DEBUG"; 

  @Override
  public void onReceive(Context context, Intent intent) {
  Toast.makeText(context, "Push received!!!!.",Toast.LENGTH_LONG).show();
  ParseAnalytics.trackAppOpened(intent);
try {
  String action = intent.getAction();
  String channel = intent.getExtras().getString("com.parse.Channel");
  JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

  Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
  Iterator itr = json.keys();
  while (itr.hasNext()) {
    String key = (String) itr.next();
    Log.d(TAG, "..." + key + " => " + json.getString(key));
  }
} catch (JSONException e) {
  Log.d(TAG, "JSONException: " + e.getMessage());
}

} } }}

Figured it out based on this push notification demo! 基于此推送通知演示了解它! https://github.com/ahiraz/pushNotificationDemo https://github.com/ahiraz/pushNotificationDemo

In your manifest file you are using 在您正在使用的清单文件中

com.parse.GcmBroadcastReceiver com.parse.GcmBroadcastReceiver

instead of that use your custom broadcastreciver , in you case simple change > com.parse.GcmBroadcastReceiver to 而不是使用你的自定义broadcastreciver,在你的情况下简单的更改> com.parse.GcmBroadcastReceiver到

com.yourpackagename.IncomingReceiver com.yourpackagename.IncomingReceiver

Dont test broadcastreciver in simulator it will not work so check in andorid device 不要在模拟器中测试broadcastreciver它不会工作所以检查andorid设备

See this repo which uses a custom broadcastreciver for parse push notification https://github.com/srini-hashinc/IIYO/tree/master 请参阅此repo,它使用自定义broadcastreciver进行解析推送通知https://github.com/srini-hashinc/IIYO/tree/master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM