public class GcmIntentService extends IntentService {
private static final String TAG = "GcmIntentService";
private NotificationManager mNotificationManager;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
Log.d(TAG,"messageType: " + messageType + ",body:" + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
Log.d(TAG,"messageType: " + messageType + ",body:" + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
Log.d(TAG,"messageType: " + messageType + ",body:" + extras.toString());
String mess = extras.getString("message");
String push_type_str = extras.getString("type");
int push_type = 0;
push_type = Integer.parseInt(push_type_str);
//通知バーに表示
sendNotification(mess, push_type);
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg, int push_type) {
mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent main_intent = new Intent(this, MainActivity.class);
// Push通知からの起動後に利用するパラメータ設定
Bundle bundle = new Bundle();
bundle.putString("message", msg);
bundle.putInt("push_type", push_type);
main_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_CLEAR_TOP);
main_intent.putExtras(bundle);
PendingIntent contentIntent;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
contentIntent = PendingIntent.getActivity(this, push_type, main_intent, PendingIntent.FLAG_CANCEL_CURRENT);
}
else {
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
taskStackBuilder.addNextIntent(main_intent);
contentIntent = taskStackBuilder.getPendingIntent(push_type, PendingIntent.FLAG_CANCEL_CURRENT);
}
int notification_id = push_type;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setWhen(System.currentTimeMillis())
.setContentTitle("title")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.app_icon);
}
else {
mBuilder.setSmallIcon(R.drawable.push_small_icon); // 白ベース画像
}
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
// push_typeは3種類のタイプがあり、push_typeによりPush起動後の処理を変更する。
// push_type毎にpush通知を受けられるようにする
mNotificationManager.notify(notification_id, mBuilder.build());
}
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private NotificationManager mNotificationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Bundle bundle = getIntent().getExtras();
if (bundle == null) {
int flag = getIntent().getFlags();
if ((flag & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
finish();
return;
}
}
else {
NotificationManager mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
int push_type = bundle.getInt("push_type", -1);
if (push_type != -1) {
mNotificationManager.cancel(push_type); // ★★★通知が消えない!!!★★★ ※cancelAll()にしても消えない
}
}
/// いろいろな処理
}