teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

onMessageReceivedファイルを追加

2020/05/22 04:04

投稿

ludolf
ludolf

スコア39

title CHANGED
File without changes
body CHANGED
@@ -13,4 +13,62 @@
13
13
  (それと良ければ前回投稿した質問に回答が得られていないのでご教授頂ければ幸いです、、)
14
14
  前回の質問:[https://teratail.com/questions/256826](https://teratail.com/questions/256826)
15
15
 
16
- ご回答よろしくお願いいたします。
16
+ ご回答よろしくお願いいたします。
17
+
18
+
19
+ 下記コードはonMessageReceivedが書いてあるファイルです。
20
+ ※細かいところは省略しています。
21
+ MyFirebaseMessagingService.java
22
+ ```Java
23
+ public class MyFirebaseMessagingService extends FirebaseMessagingService {
24
+
25
+ private static final String TAG = "MyFirebaseMsgService";
26
+
27
+ @Override
28
+ public void onNewToken(String token) {
29
+ …省略
30
+ }
31
+
32
+ @Override
33
+ public void onMessageReceived(RemoteMessage remoteMessage) {
34
+
35
+ Log.d(TAG,"プッシュ通知が来た");
36
+
37
+ if (remoteMessage != null) {
38
+ // 通知メッセージ
39
+ RemoteMessage.Notification notification = remoteMessage.getNotification();
40
+
41
+ if (notification != null) {
42
+ // 通知メッセージを処理
43
+ Log.d(TAG,notification.getTitle());
44
+ Log.d(TAG,notification.getBody());
45
+ Log.d(TAG,notification.getClickAction());
46
+
47
+ String imageUrl;
48
+
49
+ if(notification.getImageUrl() != null){
50
+ Log.d(TAG,notification.getImageUrl().toString());
51
+ imageUrl = notification.getImageUrl().toString();
52
+ }else{
53
+ Log.d(TAG,"なにも入っていません");
54
+ imageUrl = "";
55
+ }
56
+
57
+ //プッシュ通知をデータベースに入れ込み
58
+ PushList pushList = new PushList();
59
+ PushListInsertAsync pushListInsertAsync = new PushListInsertAsync(notification.getTitle(),notification.getBody(),dateTime,notification.getClickAction(),1,0,imageUrl,getApplicationContext(),pushList);
60
+ pushListInsertAsync.execute();
61
+
62
+ }
63
+
64
+ //フォアグランド用にタイトル・本文・チャンネルを作成
65
+ String title = notification.getTitle();
66
+ String body = notification.getBody();
67
+ String channel = notification.getChannelId();
68
+
69
+ //Push通知表示用メソッド呼び出し
70
+ sendNotification(title,body,channel);
71
+
72
+ }
73
+ }
74
+ ```