在云功能和Android设备上复制Firebase通知

时间:2018-09-04 15:23:57

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

我面临一个奇怪的问题,notification一键发送两次,firebase cloud functions的日志发送两次,移动设备两次。  我正在使用2条通知。 “第二”是双倍的,而“第一”是发送一次。 这是我的Firebase云功能部分

const functions = require('firebase-functions');
const admin=require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const api = admin.firestore()
api.settings({timestampsInSnapshots: true})





            exports.first=functions
           .firestore
             .document("System/{id}/ProfileAds/{adid}")
.onWrite((change,context)=>{
const id=context.params.id;
const adid=context.params.adid;
    //console.log("Users id  "+ id +"|notification id  "+ adid );

    //INFORMATION FROM THE AD
    return admin.firestore()
     .collection('System').doc(id)
      .collection('ProfileAds').doc(adid)
    .get().then(queryResult=>{
        const myAdNametxt=queryResult.data().myAdNametxt;
        const companyName=queryResult.data().companyName;
       //return console.log("title " + myAdNametxt + "company name : " 
        +companyName); 
        from=admin.firestore()
       .collection('System').doc()
          .collection('following').doc(adid).get();
        const payload= {
                        notification: {
                            title : "وارد اليوم من : " + companyName,
                            body : myAdNametxt,
                            icon : "default",
                            click_action : "NOTIFICATIONTARGER.COM"
                        },
                        data :{
                            id :id

                        }
                    };
              return admin.messaging()
              .sendToTopic(id,payload)
              .then(result=>{
return console.log("Notification sent from "+ companyName +"and contains : 
             "+ companyName 

    );
      });

    });
});


exports.second=functions.firestore
.document('System/{id}/ProfileOrders/{orderid}')
.onWrite((change,context)=>{
const orderid=context.params.orderid;
const id=context.params.id;
return 
   admin.firestore()
    .doc(orderid).get().then(queryResult=>{
    const companyName=queryResult.data().companyName;
    const orderTitle=queryResult.data().orderTitle;

    const payload= {
        notification :{
            title : "مطلوب أوردر من : " +companyName,
            body : orderTitle,
            icon : "default",
            click_action : "NOTIFICATIONT.COM"         
           },
        data : {
                id :id
        }
    };
    const x=id+"1";
    return admin.messaging().sendToTopic(x,payload).then(result=>{
        return console.log("Order sent from "+ id + " and contains : "+ 
        orderTitle 
    );

    });
});
});

和Java代码

public class
FirebaseMessagingService
extends com.google.firebase.messaging.FirebaseMessagingService {

 @Override
 public void onMessageReceived(RemoteMessage remoteMessage) {
 super.onMessageReceived(remoteMessage);

 int id = (int) System.currentTimeMillis();
 String messageTitle = remoteMessage.getNotification().getTitle();
 String messageBody = remoteMessage.getNotification().getBody();
 String click_action=remoteMessage.getNotification().getClickAction();


 String companyposition=remoteMessage.getData().get("id");
 //String dataFrom=remoteMessage.getData().get("fromuserid");

 NotificationCompat.Builder builder =
     new NotificationCompat
    .Builder(this, getString(R.string.default_notification_channel_id))
    .setSmallIcon(R.drawable.m)
    .setContentTitle(messageTitle)
    .setContentText(messageBody);

 Intent resultIntent = new Intent(click_action);
 resultIntent.putExtra("CompanyPosition",companyposition);
 PendingIntent pendingIntent =
     PendingIntent

    .getActivity(


        this,
        0,
        resultIntent,
        PendingIntent.FLAG_UPDATE_CURRENT

    );
 builder.setContentIntent(pendingIntent);
 //____FOR OREO AND HIGHER VERSIONS_____
 if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
      int importance = NotificationManager.IMPORTANCE_HIGH;
      String channelID = BuildConfig.APPLICATION_ID;
      NotificationChannel channel = new NotificationChannel
     (getString(R.string.default_notification_channel_id), BuildConfig.APPLICATION_ID, importance);
      channel.setDescription(channelID);
      NotificationManager notificationManager = getSystemService(NotificationManager.class);
      //assert notificationManager != null;
      notificationManager.createNotificationChannel(channel);
 }


 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 assert notificationManager != null;
 notificationManager.notify(id, builder.build());
 }
  }

manifestFile

 <activity android:name=".AdsOfFollowingActivity">
        <intent-filter>
            <action android:name="NOTIFICATIONTARGER.COM" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name=".BasicActivity" />
    <activity android:name=".OrdersActivity" />
    <activity android:name=".MyOrdersActivity" />
    <activity android:name=".CustomersActivity"/> 
    <activity android:name=".CustomerActivity">
        <intent-filter>
            <action android:name="NOTIFICATIONT.COM" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>


    </activity>

0 个答案:

没有答案