2015-08-21 30 views
0

我使用的解析做推送通知和我遇到的問題是,我的應用程序運行時(無論是在前臺或後臺)的手機的操作系統不顯示在通知欄推送通知。我需要做什麼更改才能看到通知欄上的推送顯示?如何在應用程序運行時在通知欄中顯示分析推送通知?

我的擴展應用類具有的onCreate()

// initialize Parse SDK 
Parse.initialize(this, Constants.APPLICATION_ID_DEBUG, Constants.CLIENT_KEY_DEBUG); 
ParsePush.subscribeInBackground(Constants.CHANNEL, new SaveCallback() { 
    @Override 
    public void done(ParseException e) { 
     if (e == null) { 
      Logger.i(TAG, "successfully subscribed to broadcast channel"); 
     } else { 
      Logger.e(TAG, "failed to subscribe for push: " + e); 
     } 
    } 
}); 
ParseInstallation.getCurrentInstallation().saveInBackground(); 

下面我在系統中了個手勢,我的應用程序,所以我用的ID登錄的用戶的頻道訂閱用戶。因此,在我的應用程序的第一個Activity中,我調用了onCreate()中的以下代碼片段。

private void registerNotifications() { 
     List<String> arryChannel = new ArrayList<String>(); 
     arryChannel.add(session.id); 

     ParseInstallation parseInstallation = ParseInstallation.getCurrentInstallation(); 
     parseInstallation.put("channels", arryChannel); 
     parseInstallation.saveEventually(); 
} 

我也有一個自定義接收器正在工作。每次發送一個推送時,都會通過onPushReceive方法接收,但是,我希望推送顯示在通知欄中。

public class ParsePushReceiver extends ParsePushBroadcastReceiver { 
    private static final String TAG = ParsePushReceiver.class.getSimpleName(); 

    @Override 
    public void onPushOpen(Context context, Intent intent) { 
     Log.i(TAG, "onPushOpen"); 
    } 

    @Override 
    protected void onPushReceive(Context context, Intent intent) { 
     Log.i(TAG, "onPushReceive"); 
    } 
} 

在此先感謝!

回答

0

我已經想通了這一點。雖然桑德拉提供的答案會讓推送通知出現在通知欄上,它沒有連接解析。

NotificationCompat.Builder mBuilder = 
    new NotificationCompat.Builder(this) 
    .setSmallIcon(R.drawable.notification_icon) 
    .setContentTitle("My notification") 
    .setContentText("Hello World!"); 

這會導致問題,因爲如果你點擊那個通知接收器如果你創建擴展ParsePushBroadcastReceiver將不會註冊onPushOpen。我的一切的實現是正確的,我只需要添加

super.onPushReceive(context, intent); 

這將使該通知出現在通知欄上,也註冊點擊。

因此,請務必讓你的接收器看起來是這樣的(至少)

public class ParsePushReceiver extends ParsePushBroadcastReceiver { 
    private static final String TAG = ParsePushReceiver.class.getSimpleName(); 

    @Override 
    public void onPushOpen(Context context, Intent intent) { 
     Log.i(TAG, "onPushOpen"); 
    } 

    @Override 
    protected void onPushReceive(Context context, Intent intent) { 
     Log.i(TAG, "onPushReceive"); 
     **super.onPushReceive(context, intent);** 
    } 
} 
1

只需卸下onPushReceive方法和默認行爲將保持(顯示在狀態欄上的通知。 您收到此行爲,因爲如果應用程序正在運行的解析推送通知會調用該方法onPushReceive,什麼也不做。

+0

我很好奇,是不是標準的實施採取收到推送通知,然後以編程方式建立的通知,並推動它?我原以爲這將由操作系統來處理。 – portfoliobuilder

+0

儘管如此,謝謝! – portfoliobuilder

+0

唯一的問題是,點擊推送通知沒有任何操作。它不會調用onPushOpen()。我相信我需要採用相同的概念,除了使用Parse發送推送通知外。 – portfoliobuilder

相關問題