AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 | <service android:name=".MyFcmListenerService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> <service android:name=".MyInstanceIDListenerService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter> </service> | cs |
MyFcmListenerService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | public class MyFcmListenerService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage message) { Map<String, String> data = message.getData(); String time = data.get("time"); String msg = data.get("message"); sendPushNotification(time, msg); } private void sendPushNotification(String title, String message) { Intent intent = new Intent(this, LoginActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); long[] pattern = {200, 100, 500, 300}; Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.icon) .setContentTitle(title) .setContentText(message) .setAutoCancel(true) .setVibrate(pattern) .setSound(defaultSoundUri) .setLights(000000255, 500, 2000) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); wakelock.acquire(5000); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); } } | cs |
MyInstanceIDListenerService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class MyInstanceIDListenerService extends FirebaseInstanceIdService { @Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken); // If you want to send messages to this application instance or // manage this apps subscriptions on the server side, send the // Instance ID token to your app server. sendRegistrationToServer(refreshedToken); } private void sendRegistrationToServer(String refreshedToken) { } } | cs |
'이것저것 > 자바*안드로이드' 카테고리의 다른 글
안드로이드 dialog setTextSize (0) | 2017.11.06 |
---|---|
JAVA Google Geocoding API (0) | 2017.02.20 |
안드로이드 EditText 다음 버튼 원하는 포커스 이동 (0) | 2017.01.23 |
안드로이드 scrollview 스크롤바 보이기 (0) | 2017.01.23 |
안드로이드 xml 에서 inputType (0) | 2017.01.19 |