Reputation: 21
I've implemented FCM notification system in my app, my problem is which it works fine only if application is running or in background, but if it is killed notifications do not appear, i've this code
Manifest.xml
<service android:name="com.Ryuk.listenme.FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
______________________________________________________________
myServer.php
function notify($tokens,$message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = mycode',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
______________________________________________________________
FirebaseMessagingServer.java //which works only in running/background
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService
{
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
showNotification(remoteMessage.getData().get("message"));
}
private void showNotification(String message)
{
Intent i = new Intent ();
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("Test FCM")
.setContentText(message)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
}
I dont' know if problem is in my application or in my message format (in php), I tried so many solutions on google but they were realistic a year ago and they did not work
Upvotes: 2
Views: 4884
Reputation: 21
I've solved it, it was a setting in my phone which prevents notifications
Upvotes: 0
Reputation: 3916
Once an app is force closed you will not receive background intents. This includes GCM/FCM messages.
"Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications."
"Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stopped applications." https://developer.android.com/about/versions/android-3.1.html#launchcontrols
GCM push notification works after app Force Stop?
Fun fact, Samsung devices often kill background apps with an "Optimize Apps" feature. This may affect your app. See here for more info - Samsung "App optimisation" feature kills background applications after 3 days
Upvotes: 2
Reputation: 3673
According to docs.
When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.
This includes messages that contain both notification and data payload. In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
When you are in background
, FCM will show notification in system tray based on the info from notification payload. Title, message, and icon are fetched from the notification payload. For more refer to this question.
Upvotes: 1