2015-05-26 119 views
0

我的目标是让用户向其他用户发送推送通知。解析推送通知从服务器发送但未被客户端接收

为了激发我的推送通知,我将一些数据加载到哈希映射中,并将它发送给某个云代码,然后验证数据并将其转发给目标用户。我查看了服务器日志,并且可以验证云代码是否正确接收了数据并发送了成功的请求,但是,我的广播接收方从未收集发送给目标用户的数据。我测试过,如果我直接从Parse发送推送通知,通过facebookId进行过滤,那么设备将收到推送通知。

我跟着这个教程:http://blog.parse.com/learn/engineering/the-dangerous-world-of-client-push/

这里是我的客户端发送代码:

ParseQuery<ParseUser> parseQuery = ParseUser.getQuery(); 
parseQuery.whereEqualTo("facebookId", userId); 
HashMap<String, Object> params = new HashMap<String, Object>(); 
try { 
     ParseUser matchUser = parseQuery.getFirst(); 
     params.put("name", matchUser.getString("name")); 
     params.put("recipientId", matchUser.getObjectId()); 
     params.put("facebookId", matchUser.getString("facebookId")); 
     params.put("startDate", new Date(matchEvent.getLong("startDate")).toString()); 
     params.put("endDate", new Date(matchEvent.getLong("endDate")).toString()); 
    } catch (ParseException pe) { 
     Log.e("MatchTimesArrayAdapter", "The match user is null"); 
    } 

ParseCloud.callFunctionInBackground("validatePush", params, new FunctionCallback<String>() { 
    @Override 
    public void done(String s, ParseException e) { 
     if (e == null) { 
      // Push sent successfully 
      Toast.makeText(context, "Request sent successfully", Toast.LENGTH_SHORT).show(); 
      } else { 
       e.printStackTrace(); 
      } 
     } 
}); 

这是我的云代码:

Parse.Cloud.define("validatePush", function(request, response) { 
    var senderUser = request.user; 
    var senderName = request.params.name; 
    var recipientId = request.params.recipientId; 
    var recipientUserId = request.params.facebookId; 
    var startDate = request.params.startDate; 
    var endDate = request.params.endDate; 
    var message = senderName + " would like to meet with you from " + startDate + " to " + endDate + "."; 

    if (senderUser.get("FriendsList").indexOf(recipientUserId) === -1) { 
     response.error("The recipient is not the sender's friend, cannot send push"); 
    } 
    var recipientUser = new Parse.User(); 
    recipientUser.id = recipientId; 
    var pushQuery = new Parse.Query(Parse.Installation); 
    pushQuery.equalTo("user", recipientUser); 

    Parse.Push.send({ 
     where: pushQuery, 
     data: { 
      alert: message 
     } 
    }).then(function() { 
     response.success("Request was sent successfully."); 
    }), function(error) { 
     response.error("Push failed to send with error: " + error.message); 
    }; 
}); 

这里是我的广播接收器:

public class ExerciseRequestBroadcastReceiver extends ParsePushBroadcastReceiver { 
@Override 
protected void onPushReceive(Context context, Intent intent) { 
    int notificationId = 51; 
    try { 
     String action = intent.getAction(); 
     JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data")); 
     // replace openAppIntent with event modification 
     Intent openAppIntent = new Intent(context, MatchesActivity.class); 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
     stackBuilder.addParentStack(MatchesActivity.class); 
     stackBuilder.addNextIntent(openAppIntent); 
     PendingIntent acceptIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
     PendingIntent declineIntent = null; 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context) 
       .setSmallIcon(R.drawable.ic_login) 
       .setContentTitle("Fiternity") 
       .setContentText(intent.getDataString()) 
       .addAction(android.R.drawable.ic_input_add, "Accept", acceptIntent) 
       .addAction(android.R.drawable.ic_input_delete, "Decline", declineIntent); 
     notificationManager.notify(notificationId, notificationBuilder.build()); 
    } catch (JSONException e) { 
     Log.e("ReceiverIssue", "JSONException: " + e.getMessage()); 
    } 
} 
} 

我有一种预感,它与我如何在ParseInstallation中存储数据有关。我已将facebookId保存到特定的ParseInstallation,但我有一个带有User对象的单独表格。我是否需要将ParseUser对象保存到ParseInstallation?

任何帮助非常感谢,谢谢!

回答

0

我放弃了尝试使用分析用户或安装解析为标识和使用Facebook的ID,而不是得到推送通知的工作。

var pushQuery = new Parse.Query(Parse.Installation); 
pushQuery.equalTo("facebookId", recipientId); 
+0

任何想法什么问题?如果我不是用Facebook的帐号的话,我该怎么办,我有使用解析只是resgiter用户不是从FB她EIS我的代码,它不workign http://pastie.org/10224504 – Erum

+0

使用解析安装的ID注册用户。 'Parse.initialize(this); ParseFacebookUtils.initialize(this); ParsePush.subscribeInBackground( “”,新SaveCallback(){ @覆盖 公共无效完成(ParseException的E){ 如果(E == NULL){ Log.d(TAG,“成功订阅广播频道。 “;; } else { Log.e(TAG,”无法订阅推送通知。“); } } }); ParseInstallation.getCurrentInstallation()。saveInBackground();' –

相关问题