Cocos2D Fcm Push Notification Not Working

FCM Open Activity and call fragment when the application is not running on device

Finally, I found a solution for my issue.

Well, first one, the FCM notification can't call a fragment directly.
You must use the click_action parameter in your intent.

First one Manifest.xml

<activity
android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar">
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
<intent-filter>
<action android:name="sm.boson.com.smd.MAIN" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

Here, you must add in the intent-filter section an action android:name and specify a name. In my case I used the same package name follow of ".MAIN" (Symbolizing the MainActivity but you could use some other name).

MyFirebaseMessagingService.java

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

String id = remoteMessage.getData().get("tag") ;

String click_action = remoteMessage.getNotification().getClickAction();

Intent intent = new Intent(click_action);
intent.putExtra("id", id);
intent.putExtra("push", "true");

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(getResources().getString(R.string.app_name));
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());

notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}

In the declaration of my Intent I added Extra to send some parameters to the activity.

MyActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

//ALL THE LOGIC OF YOU ONCREATE EVENT

Intent i = getIntent();
Bundle extras = i.getExtras();

if(extras != null) {
String push = extras.getString("push");
if (push != null) {
Integer id = Integer.parseInt(extras.getString("id"));
goToDetalleDenuncia(id);
}else if ( extras.getString("tag") != null ){
Integer id = Integer.parseInt(extras.getString("tag"));
goToDetalleDenuncia(id);
}else
goToMenu();
}else
goToMenu();
}

Well, when the application is running, you can get the parameter with the first if statement and get them with: extras.getString("push")

BUT, if your application is running in background (or it's closed) you must get the paramters with:

Integer.parseInt(extras.getString("tag"));

Why "tag"?
In my MyFirebaseMessagingService I get the "tag" and I put it into an "id" key in Extra for my Intent.

And my json from my server like:

{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"priority" : "normal",
"notification" : {
"body" : "This week's edition is now available.",
"title" : "Mytitle",
"icon" : "new"
},
"data" : {
"tag" : "3.21.15",
}
}

And in this way, now I can open an specific Fragment using FCM when the application is running, when is in background and when it's closed.

Hope this helps you.

Firebase Cloud Messaging Push and In-app notifications

  1. You can use room database. You save all the notifications and then show them in the fragment. If the fragment is already show, you can send and show instantly with broadcastReceiver. Room
  2. FCM token is created once. The registration token may change when:

    • The app deletes Instance ID
    • The app is restored on a new device
    • The user uninstalls/reinstall the app
    • The user clears app data.

You can retrieve the current token like this:

FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}

String token = task.getResult().getToken();

}
});


Related Topics



Leave a reply



Submit