我在android中创建了一个论坛应用程序,并使用phpmyadmin作为我的数据库。但是当一个问题得到一个新的答案时,应用程序应该向所有用户显示通知,所以我怎么能这样做是需要使用firebase还是只需使用webservice!
答案 0 :(得分:0)
首先,您需要转到firebase控制台并创建一个应用程序。 (为此您需要登录您的Google帐户)并按照此处提供的步骤操作。 https://firebase.google.com/docs/
完成后,您需要将这些服务添加到Manifest.xml文件
<service
android:name=".firebase.FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".firebase.FirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
FirebaseInstanceIdService.class
public class FirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseInstanceIDService";
@Override
public void onTokenRefresh() {
String token = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "OnTokenRefresh callback. Token received : " + token);
}
}
FirebaseMessagingService.class
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FirebaseMessagingService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG,"onMessageReceived.");
showNotification(remoteMessage.getData().get("message"));
}
private void showNotification(String message) {
Intent i = new Intent(this, HomeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("Slapr Notification Demo")
.setContentText(message)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
}
现在,您可以通过执行以下操作获取活动中的tokenId
Log.d(TAG, "Recieved token : " + FirebaseInstanceId.getInstance().getToken());
这是我开始时找到的最有用的教程。我希望它对你有所帮助。 https://www.youtube.com/watch?v=LiKCEa5_Cs8