应用程序在前台运行时未收到通知

时间:2018-05-30 15:51:40

标签: android firebase firebase-cloud-messaging

我正在使用firebase向用户发送通知。当应用程序处于后台时它可以正常工作,但在前台时不会显示通知。不确定缺少什么。我在堆栈溢出中完成了所有答案但没有任何帮助下面是代码:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String notificationTitle = null, notificationBody = null;



if (remoteMessage.getData().size() > 0) {
Log.d(REGTAG, "Message data payload: " + remoteMessage.getData());

//sendNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("body"));
}

// Check if message contains a notification payload.

if (remoteMessage.getNotification() != null) {
Log.d(REGTAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
notificationTitle = remoteMessage.getNotification().getTitle();
notificationBody = remoteMessage.getNotification().getBody();
}


final String finalNotificationTitle = notificationTitle;
final String finalNotificationBody = notificationBody;


sendNotification(finalNotificationTitle, finalNotificationBody);


}private void sendNotification(final String notificationTitle, final String notificationBody) {mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull final FirebaseAuth firebaseAuth) {//to check if user is logged in and whether its the same user who has sent the messageif (login_status && (!firebaseUser.getUid().isEmpty() && !user_id.contentEquals(firebaseUser.getUid())) {Intent intent = new Intent(MyFirebaseMessagingService.this, AlertsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setAction(Long.toString(System.currentTimeMillis()));
pendingIntent(notificationTitle, notificationBody, intent, INT = 100);}mAuth.addAuthStateListener(mAuthListener);}public void pendingIntent(String notificationTitle, String notificationBody, Intent intent , final int INT){

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(intent);

PendingIntent pendingIntent=stackBuilder.getPendingIntent(INT, PendingIntent.FLAG_CANCEL_CURRENT);


final NotificationCompat.Builder notificationBuilder;
notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setAutoCancel(true) //Automatically delete the notification
.setSmallIcon(R.mipmap.ic_launcher) //Notification icon
.setContentIntent(pendingIntent)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
;

mAuth = FirebaseAuth.getInstance();
firebaseUser=mAuth.getCurrentUser();

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(INT, notificationBuilder.build());

}}

以下是Payload :(来自index.js的片段)

exports.sendNotificationAlert = functions.database.ref(`path`).onWrite((change,context) => {

const getDeviceTokensPromise = admin.database().ref(`/Token/token_no`).once('value');

const getBody=admin.database().ref(`/Alert`).once('value');

var title_input='new Alert';

var contentAlert = change.after.val();

var body_input=contentAlert.description;

//const tokensSnapshot = results[0];

token_send(admin,title_input,body_input,getBody,getDeviceTokensPromise,change);


});


const payload = {

data: {

title: title_input,

body: body_input

//icon: follower.photoURL

},

notification: {

title: title_input,

body: body_input

}

};

0 个答案:

没有答案