在Firebase Console中使用您的工程包名称创建一个unity工程,详情请参加FCM指南。
接下来从https://firebase.google.com/download/unity下载Firebase Unity SDK,并将此SDK添加到您的应用中。
在unity中选择Assets > Import Package > Custom Package菜单。
从导入刚刚下载的Firbase Unity SDK 中的FirebaseMessaging.unitypackage
package。
当Import Unity Package 窗口出现时,点击Import按钮。
在Assets/TapjoySample/Scripts下的TapjoySample.cs代码中取消下面这行的注释
#define USE_FIREBASE_MESSAGING
在您工程代码中设置Firebase 云消息之前,使用如下代码初始化Firebase SDK:
Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
var dependencyStatus = task.Result;
if (dependencyStatus == Firebase.DependencyStatus.Available) {
// Create and hold a reference to your FirebaseApp, i.e.
// app = Firebase.FirebaseApp.DefaultInstance;
// where app is a Firebase.FirebaseApp property of your application class.
// Set a flag here indicating that Firebase is ready to use by your
// application.
} else {
UnityEngine.Debug.LogError(System.String.Format(
"Could not resolve all Firebase dependencies: {0}", dependencyStatus));
// Firebase Unity SDK is not safe to use here.
}
});
在您的代码中设置Firebase 云消息:
public void Start() {
Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}
public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token) {
UnityEngine.Debug.Log("Received Registration Token: " + token.Token);
}
public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e) {
UnityEngine.Debug.Log("Received a new message from: " + e.Message.From);
}
注意: Firebase SDK要添加到unity工程的project级别而不是platform级别。尽管我们没有在unity iOS中实现FCM,我们还是需要将 Google_service.plist添加到 Assets。在Firebase Console添加一个iOS App创建可以得到Google_service.plist,下载并添加到 Assets。
具体请参考: https://firebase.google.com/docs/cloud-messaging/unity/client
在发送push notification前,你需要在Tapjoy控制面板中设置API server key and sender ID。请点击控制面板上方的”Setting - App Setting”,然后在左菜单栏中选择”Push Certificate”来配置您的API key和Sender ID。
您可以在Google API Console上找到您的Sender ID和API Key。具体参见How to find sender id and api key for gcm。
由于Unity FCM 实现,MessageReceived delegation 无法在应用在后台或者非激活状态下唤醒。为了能使应用在后台或者非激活状态下收到推送消息,您需要在Android 代码中添加如下实现:
import com.google.firebase.messaging.RemoteMessage;
import com.google.firebase.messaging.cpp.ListenerService;
import com.tapjoy.Tapjoy;
public class FCMMessageService extends ListenerService {
@Override
public void onMessageReceived(RemoteMessage message) {
// super.onMessageReceived(message);
Tapjoy.setReceiveRemoteNotification(getApplicationContext(), message.getData());
}
}
<service android:name=".pushnotification.FCMMessageService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
当你通过Tapjoy推送消息的时候您的应用图标将会被显示作默认通知的图标。
你也可以通过在AndroidManifest.xml文件中设置<application>
tag来更换您的图标:
<manifest ...>
...
<application ...>
...
<meta-data android:name="com.tapjoy.notification.icon" android:resource="@drawable/ic_notify"/>
...
</application>
...
</manifest>
在SDK11.9.0以及之后的版本,我们提供API使您能挑选用户发送推送通知,您可以查询是否开启对于某个用户的推送通知:
/**
* @brief Returns true if the push notification is disabled when target platform is Android
* Returns false otherwise
*/
public static bool IsPushNotificationDisabled()
/**
* @brief Sets whether the push notification is disabled. (Only for Android)
* @param disabled
* true to disable the push notification
*/
public static void SetPushNotificationDisabled(bool disabled)
Tapjoy Unit Plugin没有为iOS推送提供C#接口,所以您需要在Unity输出的Xcode工程中修改。
要支持push notification,在下列委托方法中调用特定的Tapjoy方法:
// called when the remote notification is registered
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[Tapjoy setDeviceToken:deviceToken];
}
// called when the user get push message while playing the app
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
[Tapjoy setReceiveRemoteNotification:userInfo];
}
别忘了注册推送服务,否则您将看不到允许的确认窗口。
// Registering for remote notifications
if (NSClassFromString(@"UNUserNotificationCenter")) {
// iOS 10+ Notifications
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[application registerForRemoteNotifications];
});
}
}];
} else if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
// iOS 8 - 9 Notifications
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
更多详细信息参见iOS Local and Remote Notification Programming Guide.
发送push notifications需要有Apple push notification service认证。参考Local Notifications and Remote Notifications Guide以获得更多信息。当您取得了认证后,上传其到Tapjoy控制面板。阅读How to Configure Push Certificates了解更多信息。请点击控制面板左下方的“Setting”然后选择左菜单栏的”Push”来上传您的认证。
我可以在使用Tapjoy的同时选择另一个push提供商或者我自己的push服务吗?
可以. 请参考 文章
我的push认证快要到期了。
请在到期前替换一个新的认证。
我能否引导用户到应用中某个特定的位置?
可以。使用定制的载荷,您可以引导用户到应用中某个特定的位置。请参考 文章.