6

プッシュ通知にGoogleクラウドメッセージング(GCM)サービスを使用する最初のAndroidアプリに取り組んでいます。サーバーアプリケーションからメッセージを正常に送信し、クライアントアプリのGCMIntentServiceクラス内のonMessageイベントにメッセージの内容を記録できるようになりました。ただし、メッセージが受信されたことを示す視覚的な表示がデバイスに表示されません。iPhoneの場合と同様に、メッセージが電話のプルダウン通知リストに表示されることを期待していました。これは手動でコーディングする必要がありますか?また、現在アクティブなアクティビティに関係なく、またアプリがバックグラウンドでアイドル状態であるかどうかに関係なく、メッセージを表示するための一般的な方法はありますか?助けていただければ幸いです。

4

2 に答える 2

7

このコードは、画面上部のAndroidシステムバーに通知を生成します。このコードは、トップバーの通知をクリックした後、ユーザーを「Home.class」に誘導する新しいインテントを作成します。現在のアクティビティに基づいて特定の処理を実行したい場合は、GCMIntentServiceから他のアクティビティにブロードキャストリクエストを送信できます。

Intent notificationIntent=new Intent(context, Home.class);
generateNotification(context, message, notificationIntent);

private static void generateNotification(Context context, String message, Intent notificationIntent) {
    int icon = R.drawable.icon;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);

    // 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, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

この例では、R.drawableとR.Stringのリソースを使用していることに注意してください。これらのリソースは、機能するために存在する必要がありますが、アイデアが得られるはずです。ステータス通知http://developer.android.com/guide/topics/ui/notifiers/index.htmlの詳細と、ブロードキャスト受信者の詳細については、こちらをご覧ください。http://developer.android.com/reference/android/content/BroadcastReceiver.html

于 2012-09-18T18:34:30.717 に答える
1

GcmListenerServiceを使用している場合は、このコードを使用して、onMessageReceivedにsendNotification()を追加できます。

@Override
public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        sendNotification(message);
}

private void sendNotification(String message) {
        Intent intent = new Intent(this, YOURCLASS.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
        PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_park_notification)
                .setContentTitle("Ppillo Message")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
于 2016-02-19T11:24:22.430 に答える