android - 点击带有firebase的推送通知中的操作并做出 native react
<p><p>我已经在我的应用程序上实现了推送通知及其工作。
现在当有人点击应用程序正在打开的通知时。</p>
<p>除此之外,当用户按下通知时,我想做另一件事:</p>
<p><strong>1.向服务器发布请求(我想记录有人点击了通知)。</strong></p>
<p><强>2。像现在一样打开应用</strong></p>
<p>我的问题是我该怎么做?我需要在服务器端还是在 native 端实现它?
如果有人有如何做到这一点的教程(<strong>适用于 android 和 iOS,甚至适用于其中一个</strong>),那将有很大帮助!</p>
<p>我找了很多,但没有找到可以帮助我的东西。</p>
<p>我正在添加我的工具:</p>
<p><strong>在服务器端:</strong></p>
<pre><code> public void sendNotification(String token,String msg){
// This registration token comes from the client FCM SDKs.
String registrationToken =token;
// See documentation on defining a message payload.
Message message = Message.builder()
.setNotification(new com.google.firebase.messaging.Notification( null,msg))
.setToken(registrationToken).setApnsConfig(ApnsConfig.builder().setAps(Aps.builder().setSound("default").build()).build()).setAndroidConfig(AndroidConfig.builder().setNotification(AndroidNotification.builder().setSound("default").build()).build())
.build();
try {
String response = FirebaseMessaging.getInstance().send(message);
} catch (FirebaseMessagingException e) {
}
}
</code></pre>
<p><strong>关于 nativereact :</strong>
<strong>安卓系统:</strong></p>
<pre><code> public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
private static int count = 0;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
Log.d(TAG, "Notification Message TITLE: " + remoteMessage.getNotification().getTitle());
Log.d(TAG, "Notification Message BODY: " + remoteMessage.getNotification().getBody());
Log.d(TAG, "Notification Message DATA: " + remoteMessage.getData().toString());
String click_action= remoteMessage.getNotification().getClickAction();
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getTitle(),
remoteMessage.getNotification().getBody(), remoteMessage.getData());
}
//This method is only generating push notification
private void sendNotification(String messageTitle, String messageBody, Map<String, String> row) {
PendingIntent contentIntent = null;
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
//.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(contentIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(count, notificationBuilder.build());
count++;
}
}
</code></pre>
<p><strong>在 iOS 上:</strong></p>
<pre><code>@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *jsCodeLocation;
jsCodeLocation = [ jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
;
RCTRootView *rootView = [initWithBundleURL:jsCodeLocation
moduleName:@"**"
initialProperties:nil
launchOptions:launchOptions];
rootView.backgroundColor = [ initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [ initWithFrame:.bounds];
UIViewController *rootViewController = ;
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
;
return YES;
}
// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
;
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
;
}
// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
;
//NSLog(@"push-notification received: %@", notification)
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
;
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
;
}
</code></pre>
<p><strong>当我得到 token 时:</strong></p>
<pre><code>setNotification(userid){
const version =DeviceInfo.getUniqueID()
firebase.app().onReady().then(app => {
const again=AsyncStorage.getItem('deviceToken', (err, token) =>{
this.props.profileActions.updateLoginTrack()
console.log("checkingTokenFromLogin",token)
console.log('version', version)
if(token==null) {
console.log("tokenNull")
app.messaging().getToken()
.then(fcmToken => {
if (fcmToken) {
console.log('fcmtokenApp', fcmToken)
this.saveDeviceToken(fcmToken)
//need to save in database too
let deviceTokenData = {
userId: userid,
deviceUniqueId: version,
deviceToken: fcmToken,
}
this.props.profileActions.updateDeviceToken(deviceTokenData)
} else {
console.log('error with getting token')
}
})
}
</code></pre>
<p>感谢您的帮助</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><strong>解决方案:</strong></p>
<p>我找到的解决方案是</p>
<p> <a href="https://rnfirebase.io/docs/v4.3.x/notifications/receiving-notifications" rel="noreferrer noopener nofollow">right here</a> </p>
<p>我只使用了函数</p>
<pre><code>getInitialNotification()
</code></pre>
<p>使用此功能,您可以在应用关闭时触发通知。</p>
<p>如果您想在应用处于后台时触发通知,您需要使用此功能:</p>
<pre><code>onNotificationOpened()
</code></pre>
<p>这个函数我没有使用监听器(它对我不起作用),我只在</p>中使用了这个函数
<pre><code>componentDidMount
</code></pre>
<p>而不是在</p>
<pre><code>componentWillUnmount
</code></pre>
<p>希望它能帮助某人并为您节省一些时间</p></p>
<p style="font-size: 20px;">关于android - 点击带有firebase的推送通知中的操作并做出 nativereact ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/52607136/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/52607136/
</a>
</p>
页:
[1]