2

そのため、それぞれの通知方法フレームワークを利用する iOS アプリと Android アプリがあります... iOS にはプッシュがあり、Android には C2DM があります (GCM に持ち込むまで)... iOS ではすべて問題ありませんが、私はC2DM メッセージをクリックしてアプリが起動されたかどうかを検出する方法を探しています (iOS の didFinishLaunchingWithOptions の機能に似ています)。

現在、Android でプッシュ メッセージが受信されると、メッセージのペイロードに含まれるデータに基づいて必要な処理を行います。そのため、ユーザーがアプリを起動すると、そのプッシュ メッセージの内容によってエクスペリエンスが決まります。 . これは、ホーム画面/履歴のアイコンを押して起動するか、メッセージをプッシュして起動するかに関係なく当てはまります。理想的には、ユーザーがそのメッセージを選択した場合にのみこれが発生し、ユーザーがホーム/履歴画面からアプリを選択した場合は正常に起動する必要があります。

4

1 に答える 1

0

GCMIntentServiceインテントクラスのonMessageリスナーのSharedPreferencesにいくつかのデータを保存できます。結局のところ、GCMリスナーはパッケージアプリに属しています。保存する内容はアプリとメッセージペイロードによって異なりますが、必要に応じて変更できます。次に、通知をクリックしたときに起動されたアクティビティのonCreate関数で、共有設定を読んで、GCM通知からのものかどうかを確認します。SharedPreferencesに保存した変数をクリアして、次にユーザーがアプリを開いたときにコンテンツが正しく表示されるようにすることを忘れないでください。

ここに例があります。残念ながら、今は試すことができませんが、アイデアを見るのは便利です。これはG2DMと非常によく似ているため、ケースで同等のものを探す必要があります。

public class GCMIntentService extends GCMBaseIntentService {

    /*... other functions of the class */

    /**
     * Method called on Receiving a new message
     * */
    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
        String message = intent.getExtras().getString("your_message");

        // notifies user
        generateNotification(context, message);
    }

    /**
     * Issues a notification to inform the user that server has sent a message.
     */
    private static void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        // Save your data in the shared preferences
        SharedPreferences prefs = getSharedPreferences("YourPrefs", MODE_PRIVATE);  
        SharedPreferences.Editor prefEditor = prefs.edit();  
        prefEditor.putBoolean("comesFromGCMNotification", true);  
        prefEditor.commit(); 

        String title = context.getString(R.string.app_name);

        Intent notificationIntent = new Intent(context, MainActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);     

    }

}
于 2013-03-11T20:48:19.580 に答える