第9章 $fh.push
$fh.push(onNotification(e), regSuccessHandler, regErrorHandler(err), pushConfig)
サーバーに登録し、プッシュ通知の受信を開始します。
サポートされるプラットフォーム
JavaScript SDK
- Cordova
- Android SDK
- iOS Objective-C SDK
- iOS Swift SDK
.NET SDK
- Windows
- Xamarin
詳細なバージョン情報については、Supported Configurations (英語) を参照してください。
9.1. 例
Javascript
// register with the server to start receiving push notifications
$fh.push(function(e) {
// on android we could play a sound, if we add the Media plugin
if (e.sound && (typeof Media != 'undefined')) {
var media = new Media("/android_asset/www/" + e.sound);
media.play();
}
if (e.coldstart) {
// notification started the app
}
// show text content of the message
alert(e.alert);
// only on iOS
if (e.badge) {
push.setApplicationIconBadgeNumber(successHandler, e.badge);
}
}, function() {
// successfully registered
}, function(err) {
// handle errors
}, {
// optional filtering criteria
alias: "user@example.com",
categories: ["Curling", "Hurling"]
});
パラメーター
onNotification(e)関数 - 着信通知のハンドラーで、以下のプロパティーを含みます。-
alert、sound、badge- クラウド API のmessageオブジェクト内の対応オプションと同等のセマンティクス。 -
coldstart- 受信した通知がアプリケーション起動の原因である場合、true。 -
payload- クラウド API のmessage.userDataオプションに対応。
-
-
regSuccessHandler関数 - 登録が成功すると起動するコールバック。 -
regErrorHandler(err)関数 - エラーのために登録が失敗した場合に起動するコールバック。その後、文字列引数として渡されます。 pushConfigオブジェクト - オプションの設定で、受信した通知を条件のセットを用いてフィルタリング可能とします。条件のセマンティクスと使用方法についての情報は、通知の送信 を参照してください。利用可能なプロパティーは以下の通りです。-
alias文字列 - 固有ユーザーの識別子 -
categoriesアレイ - カテゴリー一覧
-
Android (Java)
サーバーに登録し、プッシュ通知の受信を開始します。
FH.pushRegister(new FHActCallback() {
@Override
public void success(FHResponse fhResponse) {
startActivity(...);
}
@Override
public void fail(FHResponse fhResponse) {
Toast.makeText(getApplicationContext(),
fhResponse.getErrorMessage(), Toast.LENGTH_SHORT).show();
finish();
}
});
メッセージを処理するために MessageHandler の実装を作成します。これを有効にするために、MessageHandler を RegistrarManager に登録します。詳細情報は、Aerogear ドキュメンテーションの Receiving notifications (英語) を参照してください。
import org.jboss.aerogear.android.unifiedpush.MessageHandler;
import org.jboss.aerogear.android.unifiedpush.gcm.UnifiedPushMessage;
public class MyMessageHandler implements MessageHandler {
@Override
public void onMessage(Context context, Bundle bundle) {
String message = bundle.getString(UnifiedPushMessage.ALERT_KEY);
}
}パラメーター
メッセージハンドラーの onMessage メソッドに渡される Bundle 内のキーです。
-
UnifiedPushMessage.ALERT_KEY- 通知テキストで、クラウド API のmessage.alertプロパティーに対応。 -
クラウド API の
message.userDataとともに送信されるすべてのキー。JSON オブジェクトを含む文字列として表示。
iOS (Objective-C)
サーバーに登録し、プッシュ通知の受信を開始します。
- (void)application:(UIApplication )application didRegisterForRemoteNotificationsWithDeviceToken:(NSData )deviceToken {
[FH pushRegister:deviceToken andSuccess:^(FHResponse success) {
NSLog(@"Push registration successful");
} andFailure:^(FHResponse failed) {
NSLog(@"Push registration Error: %@", failed.error);
}];
}処理メッセージ:
- (void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo {
NSLog(@"message received: %@", userInfo[@"aps"][@"alert"][@"body"]);
}パラメーター
userInfoNSDictionaryapsNSDictionary-
alertNSString/NSDictionary - 通知テキストで、クラウド API のmessage.alertプロパティーに対応。iOS ドキュメンテーションの Local and Remote Notification Programming Guide にあるように、タイプはNSStringまたはNSDictionaryのいずれかにすることが可能 -
クラウド API の
message.userDataとともに送信されるすべてのキー。
-
.NET (C#)
サーバーに登録し、プッシュ通知の受信を開始します。
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
try
{
await FHClient.Init();
// register with the server to start receiving push notifications
FH.RegisterPush(HandleNotification);
}
catch (Exception ex)
{
new MessageDialog("Error", ex.Message).ShowAsync();
}
...処理メッセージ:
private void HandleNotification(object sender, PushReceivedEvent e)
{
Console.WriteLine(e.Args.Message);
}パラメーター
PushReceivedEvent.Args-
Message文字列 - 通知テキストで、クラウド API のmessage.alertプロパティーに対応。 -
DataIDictionary<string, string> - クラウド API 内のmessage.userDataに渡される値のディクショナリー。
-

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.