[英]Android - Using received push notification
我一直在嘗試在我的Android應用程序上從Parse.com發出推送通知。 我還編寫了一個PHP文件來發送通知,到目前為止,它運行良好。 現在,我想知道如何將在Android應用程序中收到的消息用作變量。
我的PHP文件是這樣的:
<?php
$APPLICATION_ID = "xxxxx";
$REST_API_KEY = "xxxxx";
$MESSAGE = "Test 123";
$url = 'https://api.parse.com/1/push';
$data = array(
'where' => '{}',
'expiry' => 1451606400,
'data' => array(
'alert' => $MESSAGE,
),
);
$_data = json_encode($data);
$headers = array(
'X-Parse-Application-Id: ' . $APPLICATION_ID,
'X-Parse-REST-API-Key: ' . $REST_API_KEY,
'Content-Type: application/json',
'Content-Length: ' . strlen($_data),
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_exec($curl);
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
<head>
<meta charset="utf-8" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Send Push</title>
</head>
<body>
<?php if (isset($response)) {
echo '<h2>Response from Parse API</h2>';
echo '<pre>' . htmlspecialchars($response) . '</pre>';
echo '<hr>';
} elseif ($_POST) {
echo '<h2>Error!</h2>';
echo '<pre>';
var_dump($APPLICATION_ID, $REST_API_KEY, $MESSAGE);
echo '</pre>';
} ?>
<h2>Send Message to Parse API</h2>
<form id="parse" action="" method="post" accept-encoding="UTF-8">
<p>
<label for="app">APPLICATION_ID</label>
<input type="text" name="app" id="app" value="<?php echo htmlspecialchars($APPLICATION_ID); ?>">
</p>
<p>
<label for="api">REST_API_KEY</label>
<input type="text" name="api" id="api" value="<?php echo htmlspecialchars($REST_API_KEY); ?>">
</p>
<p>
<label for="api">MESSAGE</label>
<textarea name="body" id="body"><?php echo htmlspecialchars($MESSAGE); ?></textarea>
</p>
<p>
<input type="submit" value="send">
</p>
</form>
</body>
</html>
在我的Android應用中,我在主要活動中聲明了這一點:
Parse.initialize(this, "xxxxx", "xxxxxx");
PushService.setDefaultPushCallback(this, MainActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
ParseAnalytics.trackAppOpened(getIntent());
這在我的清單上,並帶有權限。
<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>
所以最重要的是我希望能夠做類似的事情
btn1.setText(msg);
其中msg
是推送通知中收到的消息
我可以告訴您我的工作方式,希望可以很容易地將其轉換為您的用法:
首先,我在清單中注冊了一個常規的BroadcastReceiver:
<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="mypackage.ParseSMSReceiver" >
<intent-filter>
<action android:name="mypackage.INCOMMING_PARSESMS" />
</intent-filter>
</receiver>
現在,假設ParseSMSReceiver可以處理帶有某些數據的推送通知。 這是我提取收到的一些數據的方式:
JSONObject json = new JSONObject(intent
.getExtras().getString(
"com.parse.Data"));
String sms = (String) json
.get(Constants.EXTRA_MESSAGE);
String sender = (json
.has(Constants.EXTRA_SENDER)) ? (String) json
.get(Constants.EXTRA_SENDER) : null;
最后,這是我發送通知的方式,以便包含預期數據的ParseSMSReceiver可以接收到它們:
JSONObject data = new JSONObject();
//this is the action matching my BroadcastReceiver
data.put("action", "mypackage.INCOMMING_PARSESMS");
data.put(Constants.EXTRA_SENDER, sender);
data.put(Constants.EXTRA_MESSAGE, message);
data.put(Constants.EXTRA_CHANNELS, new JSONArray(channels));
ParsePush androidPush = new ParsePush();
androidPush.setQuery(query);
androidPush.setData(data);
androidPush.setExpirationTimeInterval(tenminutes);
androidPush.sendInBackground();
您可能會看到,我直接從我的應用程序發送了推送消息,但是使用PHP的概念同樣適用於您。
希望這可以幫助
編輯:
您可以在android上測試代碼,而無需依賴您的PHP代碼。 parse.com上的儀表板可讓您直接發送JSON推送通知。 您要發送的JSON消息應類似於:
{ "action": "yourpackage.YOURACTIONSTRING", "msg" : "the message to be received" }
yourpackage.YOURACTIONSTRING應該與清單中接收者的動作android:name相匹配。
另外,要驗證是否可以接收常規推送消息,請嘗試從儀表板發送常規消息。
編輯2
檢查了您的代碼,並且在清單中定義廣播時無需注冊。 這是以前使用ParseSMSReceiver應該做的一個示例:
public class ParseSMSReceiver extends BroadcastReceiver {
private static final String TAG = ParseSMSReceiver.class.getName();
@Override
public void onReceive(final Context context, final Intent intent) {
Bundle extras = intent.getExtras();
String message = extras != null ? extras
.getString("com.parse.Data") : "";
JSONObject jObject;
try {
jObject = new JSONObject(message);
Log.d("Log",
jObject.getString("msg")
+ jObject.getString("action"));
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
ParseSMSReceiver應該駐留在正確的包中,以匹配清單中的接收者android:name。
該接收器將自動偵聽您在intent-filter部分中定義的任何操作字符串。
將“ alert”更改為“ msg”,這就是我在上面的示例中使用的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.