LoginSignup
28
29

More than 5 years have passed since last update.

AndroidでLocalPushを実装する

Last updated at Posted at 2016-06-02

今更ながら初めてLocalPushを実装する機会があったので、記録してみます。

手順

  • イベント発火側:インテントを渡してローカルPUSHのトリガーをひく
  • イベント受け取る側:インテントを受け取って通知を出す
  • AndroidManifestでの設定

「今回はボタンを押したらプッシュが来る」前提のコード

イベント発火側



private void registerLocalPush() {
        Log.d(TAG, "registerLocalPush: ");
        Intent intent = new Intent(this, LocalNotificationService.class);
        intent.setAction(ACTION_LOCAL_PUSH);
        PendingIntent sender = PendingIntent.getBroadcast(this, REQ_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        am.set(0, 0, sender);
    }
  • REQ_CODE:リクエストコード。PendingIntentを識別するためのコード

LocalNotificationService

BroadcastReceiverを敬称したクラスを作る。

public class LocalNotificationReceiver extends BroadcastReceiver {
    private static final String TAG = LocalNotificationService.class.getCanonicalName();
    public static final String INTENT_KEY_LOCAL_PUSH = "INTENT_KEY_LOCAL_PUSH";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION_LOCAL_PUSH)) {
            Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon);
            final Intent notificationIntent = new Intent(context, LocalPushActivity.class);
            notificationIntent.putExtra(INTENT_KEY_LOCAL_PUSH, true);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setWhen(System.currentTimeMillis());
            builder.setSmallIcon(R.drawable.icon);//ないと通知が届かない
            builder.setLargeIcon(largeIcon);
            builder.setTicker("ticker");
            builder.setContentTitle("タイトル");
            builder.setContentText("本文");
            final PendingIntent contentIntent = PendingIntent.getActivity(context, LocalPushActivity.REQ_CODE, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
            builder.setContentIntent(contentIntent);
            // タップで通知領域から削除する
            builder.setAutoCancel(true);
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, builder.build());
        }
    }
}
  • setSmallIcon():これを設定していないとそもそもステータスバーにプッシュされない
  • PendingIntent:時間指定して発行するインテントのこと
  • ACTION_LOCAL_PUSH:"jp.co.hoge.appname.localpush"のようなパッケージ名+アクション名が入ってくる
  • INTENT_KEY_LOCAL_PUSH:任意のキー文字列
 builder.setStyle(new NotificationCompat.BigTextStyle().bigText(context.getString(R.string.label_notification_apply_complete)));
        builder.setContentText(context.getString(R.string.label_notification_apply_complete));

上のように書くと一行の時のテキストと二行以上に渡るときの設定をかけるみたい
(ピンチで広げた時に2行表示される)
* 通知が一番上の時は2行表示される
* 他の通知が入って下に下がった時に1行に省略される

AndroidManifest

receiverの設定をする。これを書かないとonReceive()で受け取れない。


 <receiver android:name=".LocalNotificationService">
            <intent-filter>
                <action android:name="com.example.vv001292.realmsample.localpush"/>
            </intent-filter>
</receiver>

参考

http://dev.classmethod.jp/smartphone/android/android-tips-23-android4-1-notification-style/
http://mashi.exciton.jp/archives/tag/pendingintent
http://y-anz-m.blogspot.jp/2011/07/androidappwidget-pendingintent-putextra.html

28
29
3

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
28
29