答案 0 :(得分:1)
试用Firebase,以下是推送通知https://firebase.google.com/docs/cloud-messaging/的文档,或者您可以查看本教程https://www.simplifiedcoding.net/android-push-notification-tutorial-using-firebase/
答案 1 :(得分:1)
您应该使用FCM。见
1)将此行添加到build.gradle:
dependencies {
compile 'com.google.firebase:firebase-messaging:9.8.0'}
2)将此服务添加到清单
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
3)将此类添加到项目中。我建议你保存你的FCM令牌:
public class MyInstanceIDListenerService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("FirebaseService", "Refreshed token: " + refreshedToken);
SharedPreferences sharedPref = getSharedPreferences("YOUR_SETTING_NAME", Context.MODE_PRIVATE);
//There are optional steps
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("token", refreshedToken);
//notify the token update
editor.putBoolean("tokenUpdate",true);
editor.commit();
//You can send this token to your server (if you have)
sendServer(refreshedToken)
}
}
4)现在将您的应用注册到https://console.firebase.google.com/。这将生成一个JSON文件,您必须将其放在app文件夹中。
5)对于生成FCM消息,您有两种可能性:
5.1)使用默认的Firebase控制台:https://console.firebase.google.com/project/fantamanager-2017/notification/compose
5.2)构建您的服务器应用程序(如果您愿意,我可以向您发送我的Php服务器代码)
6)将此服务添加到您的Manifest.xml中
<service android:name="FcmBroadcastReceiver">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
创建一个可以拦截推送的类:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final ID_NOTIFICATION = ...
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String from,data;
from=remoteMessage.getFrom());
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
data=remoteMessage.getNotification().getBody());
}
// 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.
sendNotification(data);
}
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(ID_NOTIFICATION, notificationBuilder.build());
}
}
或者,如果您有自定义服务,请使用以下命令更改onMessageReceived函数的主体:
Map data = message.getData();
if (data.containsKey(YOUR_DATA_FIELD_1)) {
String field1= data.get(YOUR_DATA_FIELD_1).toString();
String field2= data.get(YOUR_DATA_FIELD_2).toString();
....
sendNotification(field1,field2...);
return;
}
答案 2 :(得分:0)
您尝试过部分代码还是整个代码?最后我看到,gcm的新应用程序已关闭。只有FCM可用。所以你需要按照他们的说法注册,在app中实现json文件并继续。如果您已经有一个实现了gcm的项目,并且您尝试了此代码的一部分,那么请检查该密钥是否正确并且您没有从该项目中复制它们的密钥。或者可能缺少清单文件中的某些元数据。