2015-02-07 76 views
0

我有一個應用程序,我想在其中使用解析通知。 我也有一個正確的樣本解析推送通知。如何和在哪裏可以使用推它呢? 我的解析應用程序擁有這些代碼:如何將解析通知添加到我的應用程序?

public class ParseApplication extends Application { 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     // Add your initialization code here 
     Parse.initialize(this, " ", " "); 


     ParseUser.enableAutomaticUser(); 
    ParseACL defaultACL = new ParseACL(); 

    // If you would like all objects to be private by default, remove this line. 
    defaultACL.setPublicReadAccess(true); 

    ParseACL.setDefaultACL(defaultACL, true); 
} 

也:

public class ParseStarterProjectActivity extends Activity { 
/** Called when the activity is first created. */ 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // To track statistics around application 
    ParseAnalytics.trackAppOpened(getIntent()); 

    // inform the Parse Cloud that it is ready for notifications 
    PushService.setDefaultPushCallback(this, ParseStarterProjectActivity.class); 
    ParseInstallation.getCurrentInstallation().saveInBackground(); 

} 

它工作正常,但我不知道在哪裏我可以在這個代碼添加到我的主要應用。

回答

0

現在,由於您沒有使用頻道或高級定位,您可以將代碼放在onCreate方法中,並且推送通知將起作用。通過這種方式,您可以從解析網站發送推送。

如果您想發送推送到設備的子集,您可以使用頻道(您可以讓用戶訂閱/取消訂閱併發送推送)或高級定位。通過這些方式,無論何時發生事件,您都可以從解析網站或應用程序本身發送推送到您想要的設備。

0

你需要寫一個自定義的接收器,它可以讀取你的推送通知,並因此採取相應的行動。

您可以按照以下步驟在您的應用程序中啓用Parse通知解析。我假設你已經在你的項目中包含了相關的jar文件(Parse-1.7.1.jar在我的案例中)。

步驟-1:在應用程序類的onCreate()函數,申報 Parse.initialize(此,Constants.PUSH_NOTIFICATION_APPLICATION_ID,Constants.PUSH_NOTIFICATION_CLIENT_ID);

用您的憑證替換PUSH_NOTIFICATION_APPLICATION_ID和PUSH_NOTIFICATION_CLIENT_ID。

第2步:在您的Manifest文件中包含以下行。

<service android:name="com.parse.PushService" /> 
<receiver 
    android:name="path to class.CustomReceiver" 
    android:exported="false" 
    android:icon="@drawable/notification_icon" 
    android:logo="@drawable/notification_icon" > 
    <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" /> 
     <category android:name="your package name" /> 
    </intent-filter> 
</receiver> 

步驟3:創建CustomReceiver類,如:

public class CustomReceiver extends ParsePushBroadcastReceiver { 

private final String TAG = "ParseNotification"; 
private String msg = ""; 
private String[] split = new String[2]; 

public static SharedPreferences settingsUserProfile; 
private String promoOfferUrl = null; 

public void onPushOpen(Context context, Intent 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)); 
      if(key.equals("move")) { 
       msg = json.getString(key); 
      } 
     } 
    } catch (JSONException e) { 
     Log.d(TAG, "JSON Exception: " + e.getMessage()); 
    } 

    if(msg.equals("1")) { 
    // move to Activity X 
    } else { 
    // move to Activity Y 
    } 
} 

STEP-4:寫你的推送通知,如:

{ 
"alert": "Hello World", 
"title": "bye bye", 
"move": "1" 
} 

你必須去解析用戶界面並在「編寫消息」部分中,選擇JSON作爲消息類型並粘貼上述示例。在我的情況下,我正在根據「move」參數的值進行不同的活動。您可以相應地設計您的JSON,並因此修改代碼以滿足您的需求。

希望它能解決您的問題。

相關問題