如何为从Firebase函数触发的通知设置不同的意图

时间:2018-06-23 00:37:38

标签: javascript android firebase-cloud-messaging android-notifications google-cloud-functions

我需要对一些非常重要的内容进行解释。是否可以对Firebase函数触发的通知设置不同的方法,以使它们在单击时都打开不同的活动?我尝试过这样的事情:

    public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            showNotification(remoteMessage.getData().get("name"));
        } else if (remoteMessage.getData().size() > 0) {
            showNotification1(remoteMessage.getData().get("name"));
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {

        }
    }

    private void showNotification1(String name) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("Important")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText(name)
                .setAutoCancel(true)
                .setStyle(new NotificationCompat.BigTextStyle())
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }


    private void showNotification(String name) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("Lecture Notes")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText(name)
                .setAutoCancel(true)
                .setStyle(new NotificationCompat.BigTextStyle())
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

但是这样做时,总是调用第一个方法。难道我做错了什么? 如何更好地格式化Javascript文件中的编码,以区分传入的通知类型?下面是我的Javascript文件。

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

exports.sendNotification = functions.database.ref('/Lecture_Materials/{MIS}/Systems_Devt/{Systems_DevtId}/name')
    .onWrite(( change,context) =>{

    // Grab the current value of what was written to the Realtime Database.
    var eventSnapshot = change.after.val();
    var str1 = "Lecture material uploaded(Systems Dev't): " + eventSnapshot;
    console.log(eventSnapshot);

    var topic = "Management.Information.System";
    var payload = {
        data: {
            name: str1,
        }
    };

    // Send a message to devices subscribed to the provided topic.
    return admin.messaging().sendToTopic(topic, payload)
        .then(function (response) {
            // See the MessagingTopicResponse reference documentation for the
            // contents of response.
            console.log("Successfully sent message:", response);
            return;
        })
        .catch(function (error) {
            console.log("Error sending message:", error);
        });
    });

    exports.sendNotification1 = functions.database.ref('/Lecture_Materials/{MIS}/Enterprise_Sys/{Enterprise_SysId}/name')
    .onWrite(( change,context) =>{

    // Grab the current value of what was written to the Realtime Database.
    var eventSnapshot = change.after.val();
    var str = "Lecture material uploaded(Enterprise Systems): " + eventSnapshot;
    console.log(eventSnapshot);

    var topic = "Management.Information.System";
    var payload = {
        data: {
            name: str,
        }
    };

    // Send a message to devices subscribed to the provided topic.
    return admin.messaging().sendToTopic(topic, payload)
        .then(function (response) {
            // See the MessagingTopicResponse reference documentation for the
            // contents of response.
            console.log("Successfully sent message:", response);
            return;
        })
        .catch(function (error) {
            console.log("Error sending message:", error);
        });
    });

0 个答案:

没有答案