1

ユーザーが友達リクエストを受け取った場合、またはメッセージを受け取った場合、Facebookのような友達リクエストとメッセージ用のAndroidアプリにプッシュ通知を実装したいと思います。まったく同じシナリオで通知が表示されます。Googleを試してみましたが、解決策が見つかりませんでした。 GCM および PARSE プッシュ通知で私に返信しないでください)適切なチュートリアル リンクを提供するか、貴重な回答を手伝ってください。事前に感謝します...

4

1 に答える 1

1

新しい回答が更新されました

ここで、古い非推奨の GCM の代わりに Firebase Cloud Messaging を使用する必要があります: https://firebase.google.com/docs/cloud-messaging/

古い答え:

公式ドキュメントとすべての質問への回答はこちら: http://developer.android.com/google/gcm/gs.html

以下のセクションでは、GCM 実装を設定するプロセスについて説明します。開始する前に、必ず Google Play Services SDK をセットアップしてください。GoogleCloudMessaging メソッドを使用するには、この SDK が必要です。

完全な GCM 実装には、アプリのクライアント実装に加えて、サーバー側の実装が必要であることに注意してください。このドキュメントでは、クライアントとサーバーの両方を含む完全な例を提供します。

具体的な質問がないので、今のところより良い答えを出すことはできません。わからないことを教えてください。

編集:コメントで要求されているように、これはバックグラウンドで実行されている場合でも通知を表示する方法です:

/**
 * Handling of GCM messages.
 */
public class GcmBroadcastReceiver extends BroadcastReceiver {
    static final String TAG = "GCMDemo";
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    Context ctx;
    @Override
    public void onReceive(Context context, Intent intent) {
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
        ctx = context;
        String messageType = gcm.getMessageType(intent);
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            sendNotification("Send error: " + intent.getExtras().toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            sendNotification("Deleted messages on server: " +
                    intent.getExtras().toString());
        } else {
            sendNotification("Received: " + intent.getExtras().toString());
        }
        setResultCode(Activity.RESULT_OK);
    }

    // Put the GCM message into a notification and post it.
    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                ctx.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
                new Intent(ctx, DemoActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(ctx)
        .setSmallIcon(R.drawable.ic_stat_gcm)
        .setContentTitle("GCM Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}
于 2013-07-03T21:07:56.630 に答える