FirebaseNotificationsを試しています。このドキュメントを使用して、通知を正しく機能させることができました。メッセージを受信し、MyFirebaseMessagingService
サービスクラス内から通知バーに通知を送信することができました。これは、アプリがバックグラウンドにあるか閉じている場合でも発生します。
必要なのは、通知で送信されたデータを収集し、それをSQLiteデータベースに挿入することです。私が書いたコードは、アプリがフォアグラウンドにある場合は正常に機能しますが、閉じている場合やバックグラウンドにある場合は機能しません。これが私が挿入物のために書いたものです。
DbHelper dbh=new DbHelper(this,"sample.sqlite",null,1);
SQLiteDatabase sdb=dbh.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("id","1");
cv.put("name","testname");
sdb.insert("test",null,cv);
sdb.close();
dbh.close();
これに関する助けに感謝します。前もって感謝します。
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
public class MyFirebaseMessagingService extends FirebaseMessagingService
{
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
Log.i("Tag","inside message" );
Log.i(StaticInfo.INFO, "From: " + remoteMessage.getFrom());
Log.i(StaticInfo.INFO, "Notification Message Title : " + remoteMessage.getNotification().getTitle());
Log.i(StaticInfo.INFO, "Notification Message Body : " + remoteMessage.getNotification().getBody());
insertPromotion();
sendNotification(remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageBody)
{
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Firebase Push Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
private void insertPromotion()
{
DbHelper dbh = new DbHelper(this, "sample.sqlite", null, 1);
SQLiteDatabase sdb = dbh.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("id","1");
cv.put("name","testname");
sdb.insert("test", null, cv);
sdb.close();
dbh.close();
Log.i("Tag","db closed");
}
}
通知はonMessageReceived
、アプリがフォアグラウンドにある場合にのみアプリに配信されます。アプリがバックグラウンドで実行されているか実行されていない場合、システムは通知を処理し、システムトレイに表示します。
Firebaseのドキュメントは、としてそれを説明します:
通知メッセージ-FCMは、クライアントアプリに代わってエンドユーザーデバイスにメッセージを自動的に表示します。通知メッセージには、ユーザーに表示されるキーの事前定義されたセットがあります。
データメッセージ-クライアントアプリはデータメッセージの処理を担当します。データメッセージには、カスタムのキーと値のペアのみがあります。
コードを常に呼び出す必要があるため、データメッセージを送信する必要があります。Firebaseコンソールからデータメッセージを送信することはできません。ただし、すでにアプリサーバーからメッセージを送信している場合は、データメッセージと通知メッセージを送信するプロセスは同じです。唯一の違いは、データメッセージにnotification
オブジェクトがないJSON構造にあります。データメッセージに関するドキュメントから
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加