Firebase de notification push ne fonctionne pas
J'ai développé une application android. J'ai utilisé firebase pour la notification. J'ai lu firebase documentation puis je leur ai fait respectivement. Je pourrais envoyer une notification push à un seul appareil à l'aide de InstanceID jeton. Mais je ne pouvais pas envoyer de notification push à tous les appareils. S'il vous plaît aider moi.
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
//[START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//TODO(developer): Handle FCM messages here.
//If the application is in the foreground handle both data and notification messages here.
//Also if you intend on generating your own notifications as a result of a received FCM
//message, here is where that should be initiated. See sendNotification method below.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
//[END receive_message]
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String messageBody) {
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)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
MyFirebaseInstanceIDService.java
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
//[START refresh_token]
@Override
public void onTokenRefresh() {
//Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
//TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}
//[END refresh_token]
/**
* Persist token to third-party servers.
*
* Modify this method to associate the user's FCM InstanceID token with any server-side account
* maintained by your application.
*
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
//Add custom implementation, as needed.
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
String value = getIntent().getExtras().getString(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
}
}
Button subscribeButton = (Button) findViewById(R.id.subscribeButton);
subscribeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//[START subscribe_topics]
FirebaseMessaging.getInstance().subscribeToTopic("news");
Log.d(TAG, "Subscribed to news topic");
//[END subscribe_topics]
}
});
Button logTokenButton = (Button) findViewById(R.id.logTokenButton);
logTokenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "InstanceID token: " + FirebaseInstanceId.getInstance().getToken());
}
});
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.muhammed.firebasepush">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
Vous pouvez poster votre manifeste trop
J'ai mis à jour ma question.
Si vous utilisez l'exemple directement, ça ne marchera pas. Pour cet exemple, pour travailler d'abord vous devez vous inscrire auprès de la FCM console. Créer un projet, de fournir la google-services.fichier json dans le dossier app/android dossier.
Avez-vous fait ces
firebaser ici Il y a un problème avec l'envoi de notifications par sujet qui a été résolu ce matin. Voir ce thread sur l'android-gcm groupe pour plus d'informations. Si votre problème persiste encore à l'heure actuelle, il est sans rapport avec cette panne.
J'ai mis à jour ma question.
Si vous utilisez l'exemple directement, ça ne marchera pas. Pour cet exemple, pour travailler d'abord vous devez vous inscrire auprès de la FCM console. Créer un projet, de fournir la google-services.fichier json dans le dossier app/android dossier.
Avez-vous fait ces
firebaser ici Il y a un problème avec l'envoi de notifications par sujet qui a été résolu ce matin. Voir ce thread sur l'android-gcm groupe pour plus d'informations. Si votre problème persiste encore à l'heure actuelle, il est sans rapport avec cette panne.
OriginalL'auteur Muhammed Cobanoglu | 2016-06-14
Vous devez vous connecter pour publier un commentaire.
Vous devez inclure premission sur le manifeste:
Mise à jour
Pas premission sont plus nécessaires, parce que "toutes les autorisations requises par la FCM sont maintenant ajoutés automatiquement par la bibliothèque"
Je ne peux pas voir quoi que ce soit dans la documentation qui précise que ces autorisations sont requises?
ce n'est plus nécessaire. Voir le guide de migration: developers.google.com/cloud-messaging/android/...
oui c'est vrai, je suis de la mise à jour maintenant, Merci.
OriginalL'auteur KRist
Au lieu de "Envoyer Maintenant" dans la firebase de la console web des notifications de gui essayer "Envoyer plus Tard" avec un peu de temps à l'avenir. Envoyer Maintenant peut avoir un bug où il met la date à laquelle envoyer légèrement dans le passé de sorte qu'il ne sera jamais envoyé.
OriginalL'auteur Doug Voss
J'ai essayé toutes les solutions posté ici et dans d'autres postes, mais ils n'ont pas de travail pour moi. J'ai résolu manuellement en désinstallant et en réinstallant l'appli sur mon appareil. Espérons que cette aide.
OriginalL'auteur ggagliano
Aller À https://console.firebase.google.com/ allez dans le menu des Notifications. ici, vous pouvez envoyer des messages directement à un instrument en particulier ou de tous les périphériques ou en fonction d'un sujet de votre client a souscrit.
Ce n'est pas de répondre à la question.
OriginalL'auteur Ashok
et n'oubliez pas,
J'ai eu le même problème et il est résolu en définissant activé, exportées vers le vrai, dans mon service, en essayant de faire comme moi,..
OriginalL'auteur Eka putra