parse.com - Android解析通知无法接收我们服务器的通知

  显示原文与译文双语对照的内容
0 0

我在项目中使用解析通知。

如果从我们的服务器推出了从我们的服务器推出的通知,我们无法获得通知。 此外,该服务器的代码也提供给iOS和iOS设备收到通知。

通知将从服务器推送,解析和解析将发送到Android设备。 请告知并感谢你的任何帮助。

我的清单:


 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 <uses-permission android:name="android.permission.WAKE_LOCK"/>
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
 <uses-permission android:name="android.permission.VIBRATE"/>
 <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
 <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
 <permission
 android:name="com.zukami.apps.dev.blynk.permission.C2D_MESSAGE"
 android:protectionLevel="signature"/>

 <uses-permission android:name="com.zukami.apps.dev.blynk.permission.C2D_MESSAGE"/>

 <!-- Push notification -->
 <service android:name="com.parse.PushService"/>

 <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"/>
 <category android:name="com.zukami.apps.dev.blynk"/>
 </intent-filter>
 </receiver>
 <receiver
 android:name="com.zukami.apps.dev.blynk.util.BlynkParseBroadcastReceiver"
 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>
 <!-- End of push notification -->

我的应用程序类:


 @Override
 public void onCreate() {
 super.onCreate();
 Parse.initialize(this,"YOUR_APP_ID","YOUR_CLIENT_KEY"); 
 PushService.setDefaultPushCallback(this,PushActivity.class); 
 ParsePush.subscribeInBackground("testChannel", new SaveCallback() {
 @Override
 public void done(com.parse.ParseException e) {
 if (e == null) {
 Log.e(TAG,
"successfully subscribed to the broadcast channel.");
 } else {
 Log.e(TAG,"failed to subscribe for push", e);
 }
 }
 });
 }

我的Activity 类:


@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.display_promo_activity);

 spinner = (ProgressBar) findViewById(R.id.progressBar1);
 fragment = new Fragment();
 FragmentManager fragmentManager = getSupportFragmentManager();
 FragmentTransaction transaction = fragmentManager.beginTransaction();
 transaction.replace(R.id.frame_container, fragment, PROMO_POPUP);
 transaction.commit();

//Track app opens.
 ParseAnalytics.trackAppOpened(getIntent());
 Bundle extras = getIntent().getExtras();
 if (extras!= null) {
 mData = extras.getString("com.parse.Data");
 Log.e(TAG,"DATA ORIGINAL =" + mData);

 if (mData!= null) {
 mData = mData.replace("","").trim();
 mData = mData.replace(""{","{").trim();
 mData = mData.replace("}"","}").trim();
 Log.e(TAG,"AFTER REPLACE =" + mData);
 try {
 JSONObject jObject = new JSONObject(mData);
 JSONObject alertObj = jObject.getJSONObject("alert");
 JSONObject dataObj = alertObj.getJSONObject("Data");
 action = dataObj.getString("Action");
 JSONObject innerDataObj = dataObj.getJSONObject("Data");
 id = innerDataObj.getString("id");
 } catch (JSONException e) {
 Log.e(TAG, e.getMessage());
 }

 }
 }

 }

我的广播类:


 @Override
 public void onPushOpen(Context context, Intent intent) {
 Log.e("Push","Clicked");
 Intent i = new Intent(context, PushActivity.class);
 i.putExtras(intent.getExtras());
 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 context.startActivity(i);
 }

这是服务器发出推送通知的代码:


 var jss = new JavaScriptSerializer();
 string endPoint ="https://api.parse.com/1/push";
 string applicationID ="APP_ID";
 string restAPIKey ="REST_API_KEY";
 PushNotificationAction data = new PushNotificationAction();
 data.Action ="ViewAction";
 data.alert ="Alert action for android";
 data.Data = new PushNotificationActionData()
 {
 id = ActionID
 };

 var client = new RestClient(endpoint: endPoint,
 method: HttpVerb.POST);

 client.Headers = new List<string>();
 client.Headers.Add(string.Format("X-Parse-Application-Id: {0}", applicationID));
 client.Headers.Add(string.Format("X-Parse-REST-API-Key: {0}", restAPIKey));

 PushActionNotification push = new PushActionNotification();
 push.channels = new List<string>();
 push.channels.Add("");

 push.data = new PushNotificationActionItem()
 {
 Data = jss.Serialize(data),
 content_available = 1
 };
 string jsonData = jss.Serialize(push).Replace("content_available","content-available");
 client.PostData = jsonData;
 client.ContentType ="application/json";
 var json = client.MakeRequest();

 response.Status = 1;
 return response;


public class PushNotificationAction
{
 public string Action { get; set; }
 public PushNotificationActionData Data { get; set; }
 public string alertMsg { get; set; }
 public string alert { get; set; }
}
public class PushNotificationActionData
{
 public string id { get; set; }
}

public class PushNotificationActionItem
{
 public string Data { get; set; }
 public int content_available { get; set; }
}
public class PushActionNotification
{
 public List<string> channels { get; set; }
//public string type { get; set; }
 public PushNotificationActionItem data { get; set; }
}

时间: 原作者:

0 0

我找到解决方法了 !

这个问题在JSON数据中。 推到iOS的JSON数据不能推到安卓,所以我决定划分Android和iOS的推送通知。 对于 Android,模板JSON如下所示:


{
"data": {
"Action":"ViewAction",
"Data": {
"id":"1432f1ea-33c4-4528-a695-7412a7a7499a"
 },
"alertMsg": null
 },
"title":"Alert action for android",
"alert":"Alert action for android",
"push_hash":"d265b3500c1089dc0c7c9ace251cfebb"
}

下面是简短描述:

  1. data --> 将JSOn负载作为这个JSOn键的值
  2. title --> 这个值将作为你的android通知描述
  3. 警告 --> 这里值将作为你的android通知标题。

我发布这个答案也许对于未来的某人来说会很有用。

原作者:
...