1

私はAndroidが初めてです。

現在、GCM 機能を Android アプリケーションに統合しています。サードパーティのサーバー アプリケーションからプッシュ通知をうまく受け取りました。

しかし、今私の問題は、プッシュ通知が来るたびに通知バー領域に表示され、その通知をクリックすると期待どおりに消えることです。

しかし、ユーザーがプッシュ通知を通知バーにクリックすると、ポップアップが表示され、そのポップアップに通知コンテンツが表示される機能が必要です。

アプリケーションが実行されているかどうかに関係なく、この機能が必要です。

つまり、アプリケーションが実行されていない場合、通知をクリックすると、アプリケーションの最初のアクティビティに関するアラートが自動的に表示されます。アプリケーションが既に実行されている場合は、アプリケーションの現在のアクティビティに関する警告ボックスが表示されます。

現在、私のアプリケーションには 7 つのアクティビティがあります。

4

3 に答える 3

1

GCM に従って MyGcmListenerService を使用している場合、コードは次のようになります。

private void sendNotification(String title, String body)
{
    Context context = getBaseContext();

    Intent notificationIntent = new Intent(context, <the-activity-you-need-to-call>.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_l)
            .setContentTitle(title)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setVibrate(new long[] { 1000, 1000})
            .setContentText(body)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
}
于 2015-12-23T20:49:36.060 に答える
1

このコードを使用して、通知を受け取ったときに GCMIntentService で通知を生成します

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);

    String title = context.getString(R.string.app_name);
                                                 //activity which you want to open
    Intent notificationIntent = new Intent(context, YOUR_ACTIVITY.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
   notificationIntent.putExtra("m", message);
    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;

    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

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

}
于 2013-08-19T07:21:41.713 に答える