3

PubNubを使用して、モバイルプッシュゲートウェイ経由でプッシュ通知を送信する予定です。私の計画は、登録IDが関連付けられている各ユーザーのためのチャンネルを持つことです。通知を送ろうとすると、私のPHPサーバはユーザのユニークなチャンネルを購読し、そのチャンネルにメッセージを発行して、チャンネルの登録を解除します。PubNubプッシュ通知

PubNubはプッシュ通知(PNmessage)を公開するためにPHP言語をサポートしていますか?上記のモデルはPubNub経由でプッシュ通知を送信するのに有効ですか?

答えて

1

Pubnubのプッシュ通知を購読する必要はありません。ユーザーのpubnubチャンネルにメッセージを公開し、特定のチャンネルのAndroidでプッシュ通知を有効にするだけです。プッシュ通知は、GCM通知を受け取ったのと同じ場所で受け取ります。 EXのためにあなたがメッセージを受信することができますGCMListenerServiceインサイド

Pubnub.enablePushNotificationsForChannel("410"); 

を書き、チャネルはアンドロイド側だから、410 でみましょう。あなたはあなたがコールバックのsuccessCallbackメソッド内でメッセージを受け取ることができますAndroidの側書き込み、

public void subscribe() { 
    String[] channels = {PrefUtils.getUserId(context) + "", ChatUtils.getDeliveryChannel(PrefUtils.getUserId(context)), ChatUtils.getReadChannel(PrefUtils.getUserId(context))}; 
    try { 
     this.getPubnubInstance().subscribe(channels, new Callback() { 
      @Override 
      public void successCallback(String channel, Object message) { 
       super.successCallback(channel, message); 
       if (PubnubWrapper.this.onPubnubMessageReceiveListener != null) 
        PubnubWrapper.this.onPubnubMessageReceiveListener.onMessageReceived(channel, message); 
      } 

      @Override 
      public void connectCallback(String channel, Object message) { 
       super.connectCallback(channel, message); 
      } 

      @Override 
      public void reconnectCallback(String channel, Object message) { 
       super.reconnectCallback(channel, message); 
      } 

      @Override 
      public void disconnectCallback(String channel, Object message) { 
       super.disconnectCallback(channel, message); 
      } 

      @Override 
      public void errorCallback(String channel, PubnubError error) { 
       super.errorCallback(channel, error); 
      } 
     }); 
    } catch (PubnubException e) { 
     e.printStackTrace(); 
    } 
} 

でGCM、

を使用していない場合は

。しかし、あなたはPubnubのインスタンスが動作し続けていることを確認する必要があります。これはAndroidで苦労します。

私はPubnubを使って本格的なチャットモジュールを実装しました。アプリがフォアグラウンドにあるとき、私はPubnubの購読方法を使用しています。バックグラウンドでは、私はチャンネルから退会し、プッシュ通知を有効にします。このように:

Foreground.init(this).addListener(new Foreground.Listener() { 
     @Override 
     public void onBecameForeground() { 
      PubnubWrapper.getInstance().subscribeAndDisablePushNotifications(); 
     } 

     @Override 
     public void onBecameBackground() { 
          PubnubWrapper.getInstance().unsubscribeAndEnablePushNotifications(); 

      } 
     } 
    }); 

そして、はい、これはForeground.javaです:

public class Foreground implements Application.ActivityLifecycleCallbacks { 

public static final long CHECK_DELAY = 500; 
public static final String TAG = Foreground.class.getName(); 

public interface Listener { 

    public void onBecameForeground(); 

    public void onBecameBackground(); 

} 

private static Foreground instance; 

private boolean foreground = false, paused = true; 
private Handler handler = new Handler(); 
private List<Listener> listeners = new CopyOnWriteArrayList<Listener>(); 
private Runnable check; 

/** 
* Its not strictly necessary to use this method - _usually_ invoking 
* get with a Context gives us a path to retrieve the Application and 
* initialise, but sometimes (e.g. in test harness) the ApplicationContext 
* is != the Application, and the docs make no guarantees. 
* 
* @param application 
* @return an initialised Foreground instance 
*/ 
public static Foreground init(Application application){ 
    if (instance == null) { 
     instance = new Foreground(); 
     application.registerActivityLifecycleCallbacks(instance); 
    } 
    return instance; 
} 

public static Foreground get(Application application){ 
    if (instance == null) { 
     init(application); 
    } 
    return instance; 
} 

public static Foreground get(Context ctx){ 
    if (instance == null) { 
     Context appCtx = ctx.getApplicationContext(); 
     if (appCtx instanceof Application) { 
      init((Application)appCtx); 
     } 
     throw new IllegalStateException(
      "Foreground is not initialised and " + 
      "cannot obtain the Application object"); 
    } 
    return instance; 
} 

public static Foreground get(){ 
    if (instance == null) { 
     throw new IllegalStateException(
      "Foreground is not initialised - invoke " + 
      "at least once with parameterised init/get"); 
    } 
    return instance; 
} 

public boolean isForeground(){ 
    return foreground; 
} 

public boolean isBackground(){ 
    return !foreground; 
} 

public void addListener(Listener listener){ 
    listeners.add(listener); 
} 

public void removeListener(Listener listener){ 
    listeners.remove(listener); 
} 

@Override 
public void onActivityResumed(Activity activity) { 
    paused = false; 
    boolean wasBackground = !foreground; 
    foreground = true; 

    if (check != null) 
     handler.removeCallbacks(check); 

    if (wasBackground){ 
     Log.i(TAG, "went foreground"); 
     for (Listener l : listeners) { 
      try { 
       l.onBecameForeground(); 
      } catch (Exception exc) { 
       Log.e(TAG, "Listener threw exception!", exc); 
      } 
     } 
    } else { 
     Log.i(TAG, "still foreground"); 
    } 
} 

@Override 
public void onActivityPaused(Activity activity) { 
    paused = true; 

    if (check != null) 
     handler.removeCallbacks(check); 

    handler.postDelayed(check = new Runnable(){ 
     @Override 
     public void run() { 
      if (foreground && paused) { 
       foreground = false; 
       Log.i(TAG, "went background"); 
       for (Listener l : listeners) { 
        try { 
         l.onBecameBackground(); 
        } catch (Exception exc) { 
         Log.e(TAG, "Listener threw exception!", exc); 
        } 
       } 
      } else { 
       Log.i(TAG, "still foreground"); 
      } 
     } 
    }, CHECK_DELAY); 
} 

@Override 
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {} 

@Override 
public void onActivityStarted(Activity activity) {} 

@Override 
public void onActivityStopped(Activity activity) {} 

@Override 
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {} 

@Override 
public void onActivityDestroyed(Activity activity) {} 

}

+0

とはいPubnub PHP SDKのサポートマイApplicationクラス内

public void subscribeAndDisablePush(String gcm_token) { String[] channels = {PrefUtils.getUserId(context) + "", ChatUtils.getDeliveryChannel(PrefUtils.getUserId(context)), ChatUtils.getReadChannel(PrefUtils.getUserId(context))}; getPubnubInstance().disablePushNotificationsOnChannels(channels, gcm_token); try { getPubnubInstance().subscribe(channels, new Callback() { @Override public void successCallback(String channel, Object message) { super.successCallback(channel, message); } }); } catch (PubnubException e) { e.printStackTrace(); } } public void unsubscribeAndEnablePush(String gcm_token) { String[] channels = {PrefUtils.getUserId(context) + "", ChatUtils.getDeliveryChannel(PrefUtils.getUserId(context)), ChatUtils.getReadChannel(PrefUtils.getUserId(context))}; getPubnubInstance().enablePushNotificationsOnChannels(channels, gcm_token, new Callback() { @Override public void successCallback(String channel, Object message) { super.successCallback(channel, message); Log.e("Subscribed", "YES"); } @Override public void errorCallback(String channel, PubnubError error) { super.errorCallback(channel, error); Log.e("Subscribed", "No"); } }); getPubnubInstance().unsubscribe(channels); } 

私はこれを行いますパブリッシングメッセージ。 – Embydextrous