민프
[PHP] Android, FCM, PHP을 이용해서 Push알림을 보내보자!!(2) 본문
이전 포스트에서는 PHP 를 이용해서 PUSH를 보내는 것을 알아보았는데
이번 예제에서는 상대방에게
내가 원하는 내용으로 알림을 PUSH 해보자!!
MyFirebaseMessagingService.java
private String TAG ="MyFirebaseMessagingService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String action_post_number_string = remoteMessage.getData().get("post_number");
String get_who = remoteMessage.getData().get("who");
Log.d(TAG, "action_post_number_string: " + String.valueOf(action_post_number_string));
int action_post_number = Integer.valueOf(action_post_number_string);
if (remoteMessage.getNotification() != null) { //포그라운드
sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getNotification().getTitle(),get_who,action_post_number);
}
else if (remoteMessage.getData().size() > 0) { //백그라운드
sendNotification(remoteMessage.getData().get("body"),remoteMessage.getData().get("title"),get_who,action_post_number);
}
}
private void sendNotification(String title, String message,String get_who,int action_post_number_string) {
// Log.d("sendNotification", String.valueOf(remoteMessage));
// String title = remoteMessage.getData().get("title");
// String message = remoteMessage.getData().get("message");
String who = get_who;
// String action_post_number_string = remoteMessage.getData().get("post_number");
Log.d(TAG, "action_post_number_string: " + String.valueOf(action_post_number_string));
int action_post_number = Integer.valueOf(action_post_number_string);
Log.d(TAG+ " sendNotificationtitle", String.valueOf(title));
Log.d(TAG+ " sendNotificationmessage", String.valueOf(message));
Log.d(TAG+ " sendNotificationwho", String.valueOf(who));
final String CHANNEL_ID = "ChannerID";
NotificationManager mManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final String CHANNEL_NAME = "ChannerName";
final String CHANNEL_DESCRIPTION = "ChannerDescription";
final int importance = NotificationManager.IMPORTANCE_HIGH;
// add in API level 26
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
mChannel.setDescription(CHANNEL_DESCRIPTION);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 100, 200});
mChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
mManager.createNotificationChannel(mChannel);
}
Intent mIntent = new Intent(getApplicationContext(), activity_detail_post.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mIntent.putExtra("get_data_postnumber",action_post_number);
mIntent.putExtra("FROM_INTENT","notification");
// startActivity(mIntent);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, mIntent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setSmallIcon(R.drawable.ic_launcher_background);
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setWhen(System.currentTimeMillis());
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle(message);
builder.setContentText(who);
builder.setContentIntent(pendingIntent);
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
builder.setContentTitle(title);
builder.setVibrate(new long[]{500, 500});
}
mManager.notify(0, builder.build());
}
@Override
public void onNewToken(String s) {
super.onNewToken(s);
}
}
회원에 해당하는 토큰값을 가지고와서
그 토큰값을 가지고 있는 회원에게만 PUSH를 보내는 것이다.
여기서 PendingIntent를 통해서 바로 실행할 수도 있고, 바로 실행하지 않을 수도 있다!!
'[PHP]' 카테고리의 다른 글
[PHP] Android, FCM, PHP을 이용해서 Push알림을 보내보자!! (2) | 2021.07.07 |
---|---|
[PHP] AWS EC2에 curl 설치해보자!(Xshell 7사용,Ubuntu 18.04 LTS, SSH) (0) | 2021.07.07 |
Comments