2014-05-04 92 views
3

之前,這是我設置的樣子。推送通知單擊通知

的LunchActivity具有代碼:

Parse.initialize(this, "MY_APP_ID", "MY_APP_KEY"); 
PushService.subscribe(this, "MyCity", HomeActivity.class); 
ParseInstallation.getCurrentInstallation().saveInBackground(); 

HomeActivity類是打開默認使用一個簡單的屏幕一個簡單的活動類。我也寫了一個自定義接收器。

public class CityPushReceiver extends BroadcastReceiver { 
    private static final String TAG = "CityPushReceiver"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     try { 
      JSONObject json = new JSONObject(intent.getExtras().getString(
        "com.parse.Data")); 

      Integer event_id = Integer.parseInt((String) json.get("event_id")); 

      Intent eventIntent = new Intent(context, EventResult.class); 
      eventIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      eventIntent.putExtra("event_id", event_id); 
      context.getApplicationContext().startActivity(eventIntent); 

     } catch (JSONException e) { 
      Log.d(TAG, "JSONException: " + e.getMessage()); 
     } 
    } 
} 

清單文件中有條目:

<receiver 
    android:name="com.myapp.CityPushReceiver" 
    android:exported="false" > 
    <intent-filter> 
     <action android:name="com.myapp.CITY_NOTIFICATION" /> 
    </intent-filter> 
</receiver> 

我用Python代碼推通知:

import json,httplib 
connection = httplib.HTTPSConnection('api.parse.com', 443) 
connection.connect() 
connection.request('POST', '/1/push', json.dumps({ 
     "channels": [ 
     "MyCity" 
     ], 
     "data": { 
    "action": "com.myapp.CITY_NOTIFICATION", 
     "alert": "New Event Notification", 
    "event_id": "425" 
     } 
    }), { 
     "X-Parse-Application-Id": "APP_ID", 
     "X-Parse-REST-API-Key": "API_KEY", 
     "Content-Type": "application/json" 
    }) 
result = json.loads(connection.getresponse().read()) 
print result 

預期這種設置不工作。我確實收到了設備上的通知(我正在使用AVD進行測試)。但它開啓了預期EventResult活動,即使沒有我點擊托盤中的通知。即使我在設備主屏幕上,並且應用程序僅在後臺運行,也會發生這種情況。當我在托盤點擊該通知,它會打開它被定義爲默認類HomeActivity類。

預期的行爲是打開EventResult只有當我點擊托盤中的通知。你們能告訴我需要改變什麼嗎?

回答

0

如此坦率地張貼在這裏所有的答案都在一個正確或其他情況,所以我雖然我就會把事情的角度在這裏。

關於我的具體問題,要看它是,如果你實現自己的廣播接收器,只要你收到推送也將被調用,不管代碼是在它裏面,它會被執行正確的方式。爲了確保你看到一個通知,有兩個重要的事情:

  1. 確保您使用的警報,當你發送的推送
  2. 無論是使用默認接收器通過解析給出。即在配置中,只需告訴解析要調用哪個活動,發送帶警報的推送並讓解析處理剩下的東西。它會顯示警報,並在您點擊警報時打開特定的活動。另一種方法比較複雜,那就是將你自己的接收機的所有注意事項寫入警報。

很大程度上取決於您是如何發送通知的。所以確保警報,數據,有時候這些活動都被正確提及。

2

使用這樣的:

public class Application extends android.app.Application { 

    public Application() { 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY"); 

     PushService.setDefaultPushCallback(this, MainActivity.class); 
    } 
} 

//MainActivity.java - 你需要點擊通知時打開活動。

在您的清單文件中添加此應用程序。

<application 
    android:label="@string/app_name" 
    android:name="com.parse.tutorials.pushnotifications.Application" 
    android:theme="@style/AppTheme"> 

    <activity 
     android:label="@string/app_name" 
     android:name="com.parse.tutorials.pushnotifications.MainActivity"> 

     <intent-filter> 

      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

並將此行添加到Activity類中,您需要在單擊通知時打開該行。

ParseAnalytics.trackAppOpened(getIntent()); 

檢查這樣的回答:LINK

+0

這是否意味着我只能在發送推送通知時才能打開HomeActivity?或者它只是一個默認的活動?如果我想根據不同的標準發送不同的活動怎麼辦?另外,我會嘗試這個解決方案,並分配賞金。 – rishi

+0

我嘗試了簡單通知(來自儀表板而不是來自python腳本)的代碼,它僅在應用程序打開時處於活動狀態或在後臺運行時纔有效。我從托盤中刪除了應用程序,並停止收到通知。當我重新打開應用程序時,我收到了所有待處理的通知。 – rishi

3

我發現,服用控制的通知創造是最好的Android上。如果您不推薦使用send a title or alert field,那麼parse.com android SDK將不會創建通知,您可以在推入時自己創建一個通知。

這是我用來創建通知的方法:

void CreateNotication(Context context, String title, String pushId) { 
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
      new Intent(context, MyClass.class), 0); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.icon) 
      .setContentTitle(title) 
      .setContentIntent(contentIntent); 

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    manager.notify(pushId, 1, mBuilder.build()); 
} 
+0

唯一的問題是,接收器擴展ParsePushBroadcastReceiver將不跟蹤onPushOpen(),當應用程序在後臺點擊推送通知,並且用戶點擊NotificationManager發送的通知時 – portfoliobuilder