Dmitry Matveev
Dmitry Matveev

Reputation: 5376

Firebase shows two notifications on Android

I am struggling to find the cause for the duplicate notification message on android using Firebase, Unity and sending from nodejs with the help of node-gcm.

Here is all relative code:

Unity3d client side I got:

 public class PushServiceManager : MonoBehaviour
    {
        public string Identifier;
        public string SkuId;

        public SQSParameters sqsOptions;
        private SQSManager sqs;

        // Use this for initialization
        void Start()
        {
            sqs = new SQSManager(this.sqsOptions);
            sqs.Initialize(this.gameObject);

            Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
            Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
        }

        public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
        {
            Debug.Log("Received Registration Token: " + token.Token);

            RegistrationToken t = new RegistrationToken();
            t.id_token = token.Token;

            // sends token to node server
            sqs.SendRegistrationToken(t);
        }

        public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e)
        {
            Debug.Log(e.Message.Data.ToString());
        }
    }

Nodejs server side:

pushNotification(pushRequest, tokens, callback) {

        // This is an array of firebase tokens
        let registrationTokens = tokens.map(function (token) {
            return token.token;
        });

        let message = new gcm.Message({
            timeToLive: pushRequest.ttl
        });

        let note = {

           title: 'Message title',
           icon: 'none',
           sound: 'none',
           body: 'message text'
        };

        message.addNotification(note);

        let payload = {
           data: {foo: "foo"}
        }

        message.addData(payload);

        this.sender.send(message, {registrationTokens}, callback);
    }

Result on device is:
push notifications on device

Question:
Why do I get the second empty push notification?
Tapping first one opens up a unity game as expected but tapping that empty one does not.
Where am I going wrong with this?

Thank you!

01.31.17 UPDATE:
Shutout to firebase support team for looking into this issue...
enter image description here

Upvotes: 0

Views: 1502

Answers (3)

Dmitry Matveev
Dmitry Matveev

Reputation: 5376

I found what was cause this issue...

I had AWS module included into the project which had it's own manifest file. In it there was a receiver declaration for "GCMBroadcastReceiver" in combination with service declaration for "GCMIntentService".

Removing those solved the issue

Upvotes: 0

nzmkey
nzmkey

Reputation: 26

I have the same duplicate notifications issue in my project and below is the solution.

Check your generated AndroidManifest.xml by unity build in [Your unity project folder]\Temp\StagingArea

Look for all receivers that have below line:

<action android:name="com.google.android.c2dm.intent.RECEIVE" />

In my case, I have below that added by the Amazon AWS unity plugin

<receiver android:name="com.amazonaws.unity.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
  <intent-filter>
    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
    <category android:name="com.amazonaws.unity" />
  </intent-filter>
</receiver>

and below from Firebase plugin

<receiver android:exported="true" android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:permission="com.google.android.c2dm.permission.SEND">
  <intent-filter>
    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
    <category android:name="com.bbbbbttttttgggg.sssssstttt" />
  </intent-filter>
</receiver>

The duplicated notification is caused by amazon AWS GCMBroadcastReceiver.

There are two options to fix this.

  1. Remove the receiver if you do not need it.
  2. Add android:exported="false" to the receiver.

Example:

   <receiver android:exported="false" android:name="com.amazonaws.unity.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="com.amazonaws.unity" />
      </intent-filter>
    </receiver>

Remember to make the change in the original AndroidManifest.xml that contains the receiver. Not the one in Temp\StagingArea. It should be one of the AndroidManifest.xml file inside [Your unity project folder]\Assets\Plugins\Android or it sub folder.

Upvotes: 1

Adam Ri
Adam Ri

Reputation: 517

I am facing the same problem with firebase. I am using Firebase together with Microsoft Azure Notification Hub. If I send a test message via Microsoft Notification Hub I only get one message and that message is handled by my Push Handler. Otherwise if I send push notifications through firebase console I am getting two message like you(also one is empty). One message is handled by my push service and the second one is somehow handled by "no code"! I cannot find in my code where the push notification send by firebase is handled? There must be a OnReceiveMessage method in the background which handles the notification. For me I come with following solution: I just uncommented my push handler and now I am only getting one notification, but this without sound...somehow I have to override that function and implement sound behaviour. Have you found a solution for that? The empty message is caused by your push handler the second full message is handled in the background by firebase.

I also getting two different types of message in my onReceive() method:

Microsoft: Bundle[{google.sent_time=1484748187461, from=230XXXXXXXXXX, google.message_id=0:1484748187469719%8a1048bff9fd7ecd, message=Test through Microsoft, collapse_key=do_not_collapse}]

Firebase: Bundle[{google.c.a.udt=0, google.sent_time=1484748603664, gcm.notification.e=1, google.c.a.c_id=3001008833206773803, google.c.a.ts=1484748604, gcm.n.e=1, from=230XXXXXXXXXXX, google.message_id=0:1484748603943016%8a1048bf8a1048bf, gcm.notification.body=Test through firebase, google.c.a.e=1, collapse_key=com.sub_zero.ipostcard}]

So the message received through firebase is handled one time in the background somewhere maybe in through the firebase lib and the second time I suppose by your handler. That why you getting two of them...Just uncomment your push handler and test it again.

Upvotes: 0

Related Questions