Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
31
Help us understand the problem. What is going on with this article?

More than 5 years have passed since last update.

@imp954sti

Push通知からアプリを開く

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);
    }
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
31
Help us understand the problem. What is going on with this article?