LoginSignup
31

More than 5 years have passed since last update.

Push通知からアプリを開く

Last updated at Posted at 2014-12-19
GCMIntentService.java
    @Override
    protected void onMessage(Context context, Intent intent) {
        String ticker = intent.getStringExtra("ticker");
        String title = intent.getStringExtra("title");
        String message = intent.getStringExtra("message");

        // プロセスがあればそのまま復帰、なければ起動画面から開始する
        Intent ni = new Intent(Intent.ACTION_MAIN);
        ni.addCategory(Intent.CATEGORY_LAUNCHER);
        ni.setClassName(getApplicationContext().getPackageName(), SplashActivity.class.getName());
        ni.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pi = PendingIntent.getActivity(context, 0, ni, PendingIntent.FLAG_CANCEL_CURRENT);

        // 通知を開いたときに表示されるアイコン
        NotificationCompat.Builder builder = new Builder(context);
        builder.setTicker(ticker); // ステータスバーに表示されるテキスト
        builder.setContentTitle(title); // 通知を開いたときに表示されるタイトル
        builder.setContentText(message);// 通知を開いたときに表示されるメッセージ
        builder.setSmallIcon(R.drawable.ic_launcher); // ステータスバーに表示されるアイコン設定
        builder.setAutoCancel(true); // 通知をクリックした時に自動的に通知を消すように設定
        builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
        builder.setContentIntent(pi); // 通知が選択された時に起動するIntentを設定
        Notification notification = builder.build();
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, notification);
    }

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31