2015-05-26 117 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();' –

相關問題