Phonegap Cordova中的Firebase通知(Firebase notification in Phonegap Cordova)

编程入门 行业动态 更新时间:2024-10-22 23:39:57
Phonegap Cordova中的Firebase通知(Firebase notification in Phonegap Cordova)

我正在尝试在PhoneGap中向我的应用添加通知。 为此,我正在使用这个插件( https://github.com/fechanique/cordova-plugin-fcm )。

这似乎有效。 当我在firebase中添加通知时,我会在手机中使用我设置的参数获取通知。

现在,当用户通过通知进入应用程序以对该数据采取特殊操作时,我正在尝试获取参数。

根据上面链接中的文档,我应该添加此事件:

FCMPlugin.onNotification( function(data){ if(data.wasTapped) //Notification was received on device tray and tapped by the user. alert( JSON.stringify(data) ); }else{ //Notification was received in foreground. Maybe the user needs to be notified. alert( JSON.stringify(data) ); } }, function(msg){ console.log('onNotification callback successfully registered: ' + msg); }, function(err){ console.log('Error registering onNotification callback: ' + err); } );

但是,不会显示任何警报。 我找不到任何调试方法,因为它只能在移动设备上运行(甚至不是模拟的,只有真实的)。

似乎有些不对劲? 我只需要android。 此外,我在加载应用程序时在bindEvents包含该事件。

I'm trying to add notifications to my app in PhoneGap. For that, I'm using this plugin (https://github.com/fechanique/cordova-plugin-fcm).

This seems to work. When I add a notification in firebase, I get it in the phone with the parameters I have set.

Now I'm trying to get the parameters when the user entered into the app through a notification to take an special action with that data.

According the the documentation in the link above, I should add this event:

FCMPlugin.onNotification( function(data){ if(data.wasTapped) //Notification was received on device tray and tapped by the user. alert( JSON.stringify(data) ); }else{ //Notification was received in foreground. Maybe the user needs to be notified. alert( JSON.stringify(data) ); } }, function(msg){ console.log('onNotification callback successfully registered: ' + msg); }, function(err){ console.log('Error registering onNotification callback: ' + err); } );

However, no alert is displayed. And I don't find any way to debug it, since it only runs in mobile (not even emulated, only real one).

Something seems wrong? I only need for android. Also I'm including that event in the bindEvents when the app is loaded.

最满意答案

我遇到过同样的问题。 但是我使用了这个插件firebase-plugin并对这些文件进行了更改

FirebasePluginMessagingService.java

package org.apache.cordova.firebase; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.util.Log; import android.text.TextUtils; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; import com.searchtrade.demo.MainActivity; import java.util.Map; public class FirebasePluginMessagingService extends FirebaseMessagingService { private static final String TAG = "FirebasePlugin"; /** * Called when message is received. * * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. */ @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. String title = null; String text = null; String category = null; int id = 0; if (remoteMessage.getNotification() != null) { title = remoteMessage.getNotification().getTitle(); text = remoteMessage.getNotification().getBody(); } else { title = remoteMessage.getData().get("title"); text = remoteMessage.getData().get("text"); category = remoteMessage.getData().get("category"); try { id = Integer.valueOf(remoteMessage.getData().get("id")); } catch (Exception e) { // ignore } } Log.d(TAG, "From: " + remoteMessage.getFrom()); Log.d(TAG, "Notification Message Title: " + title); Log.d(TAG, "Notification Message Body/Text: " + text); Log.d(TAG, "myNewMessageBody: " + title); // TODO: Add option to developer to configure if show notification when app on foreground if (!TextUtils.isEmpty(text) || !TextUtils.isEmpty(title)) { sendNotification(id, title, text, category,remoteMessage.getData()); } } private void sendNotification(int id, String title, String messageBody,String category, Map<String, String> data) { Intent intent = new Intent(this, OnNotificationOpenReceiver.class); Bundle bundle = new Bundle(); for (String key : data.keySet()) { bundle.putString(key, data.get(key)); } bundle.putString("myTitle",title); bundle.putString("myMessageBody",messageBody); bundle.putString("myCategory",category); // additional payload data Log.d(TAG, "myMessageBody: " + messageBody); intent.putExtras(bundle); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(getApplicationInfo().icon) .setContentTitle(title) .setContentText(messageBody) .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody)) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(id, notificationBuilder.build()); } }

OnNotificationOpenReceiver.java

package org.apache.cordova.firebase; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.util.Log; import android.widget.Toast; public class OnNotificationOpenReceiver extends BroadcastReceiver { String title,text,category; private static final String TAG = "BroadcastReceiver"; @Override public void onReceive(Context context, Intent intent) { // Toast.makeText(context,"test6",Toast.LENGTH_SHORT).show(); Intent i = new Intent(context, CustomLaunchUrl.class); /////////////// i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ////////////// Bundle data = intent.getExtras(); title = data.getString("myTitle"); text = data.getString("myMessageBody"); category = data.getString("myCategory"); Bundle dt = new Bundle(); dt.putString("finalTitle",title); dt.putString("finalBody",text); dt.putString("finalCategory",category); i.putExtras(dt); Log.d(TAG,"Notification working test: "); // Toast.makeText(context,title+" "+text,Toast.LENGTH_LONG).show(); context.startActivity(i); } }

使用自定义功能为自定义页面启动添加新文件

CustomLaunchUrl.java

package org.apache.cordova.firebase; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.widget.Toast; import org.apache.cordova.CordovaActivity; public class CustomLaunchUrl extends CordovaActivity { String x,y,z; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // load the layout Bundle d = getIntent().getExtras(); x = d.getString("finalTitle"); y = d.getString("finalBody"); z = d.getString("finalCategory"); //Toast.makeText(CustomLaunchUrl.this, x+" "+y, Toast.LENGTH_SHORT).show(); loadUrl("file:///android_asset/www/networkStats_2.html"); // change html as your need final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { // Do something after 5s = 5000ms loadUrl("javascript:set_ndata('"+x+"','"+y+"','"+z+"')"); } }, 2000); } }

在Platforms / android / AndroidManifest.xml中添加它

<activity android:name="org.apache.cordova.firebase.CustomLaunchUrl"> </activity>

并在您的www / networkStats_2.html中添加此功能

function set_ndata(x,y,z) //got payload data in html page { alert(x); alert(y); alert(z); }

I had the same issue. But I had used this plugin firebase-plugin and made changes in these files

FirebasePluginMessagingService.java

package org.apache.cordova.firebase; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.util.Log; import android.text.TextUtils; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; import com.searchtrade.demo.MainActivity; import java.util.Map; public class FirebasePluginMessagingService extends FirebaseMessagingService { private static final String TAG = "FirebasePlugin"; /** * Called when message is received. * * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. */ @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. String title = null; String text = null; String category = null; int id = 0; if (remoteMessage.getNotification() != null) { title = remoteMessage.getNotification().getTitle(); text = remoteMessage.getNotification().getBody(); } else { title = remoteMessage.getData().get("title"); text = remoteMessage.getData().get("text"); category = remoteMessage.getData().get("category"); try { id = Integer.valueOf(remoteMessage.getData().get("id")); } catch (Exception e) { // ignore } } Log.d(TAG, "From: " + remoteMessage.getFrom()); Log.d(TAG, "Notification Message Title: " + title); Log.d(TAG, "Notification Message Body/Text: " + text); Log.d(TAG, "myNewMessageBody: " + title); // TODO: Add option to developer to configure if show notification when app on foreground if (!TextUtils.isEmpty(text) || !TextUtils.isEmpty(title)) { sendNotification(id, title, text, category,remoteMessage.getData()); } } private void sendNotification(int id, String title, String messageBody,String category, Map<String, String> data) { Intent intent = new Intent(this, OnNotificationOpenReceiver.class); Bundle bundle = new Bundle(); for (String key : data.keySet()) { bundle.putString(key, data.get(key)); } bundle.putString("myTitle",title); bundle.putString("myMessageBody",messageBody); bundle.putString("myCategory",category); // additional payload data Log.d(TAG, "myMessageBody: " + messageBody); intent.putExtras(bundle); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(getApplicationInfo().icon) .setContentTitle(title) .setContentText(messageBody) .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody)) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(id, notificationBuilder.build()); } }

OnNotificationOpenReceiver.java

package org.apache.cordova.firebase; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.util.Log; import android.widget.Toast; public class OnNotificationOpenReceiver extends BroadcastReceiver { String title,text,category; private static final String TAG = "BroadcastReceiver"; @Override public void onReceive(Context context, Intent intent) { // Toast.makeText(context,"test6",Toast.LENGTH_SHORT).show(); Intent i = new Intent(context, CustomLaunchUrl.class); /////////////// i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ////////////// Bundle data = intent.getExtras(); title = data.getString("myTitle"); text = data.getString("myMessageBody"); category = data.getString("myCategory"); Bundle dt = new Bundle(); dt.putString("finalTitle",title); dt.putString("finalBody",text); dt.putString("finalCategory",category); i.putExtras(dt); Log.d(TAG,"Notification working test: "); // Toast.makeText(context,title+" "+text,Toast.LENGTH_LONG).show(); context.startActivity(i); } }

Adding new file for custom page launch with custom function

CustomLaunchUrl.java

package org.apache.cordova.firebase; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.widget.Toast; import org.apache.cordova.CordovaActivity; public class CustomLaunchUrl extends CordovaActivity { String x,y,z; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // load the layout Bundle d = getIntent().getExtras(); x = d.getString("finalTitle"); y = d.getString("finalBody"); z = d.getString("finalCategory"); //Toast.makeText(CustomLaunchUrl.this, x+" "+y, Toast.LENGTH_SHORT).show(); loadUrl("file:///android_asset/www/networkStats_2.html"); // change html as your need final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { // Do something after 5s = 5000ms loadUrl("javascript:set_ndata('"+x+"','"+y+"','"+z+"')"); } }, 2000); } }

Add this in Platforms/android/AndroidManifest.xml

<activity android:name="org.apache.cordova.firebase.CustomLaunchUrl"> </activity>

And in your www/networkStats_2.html add this function

function set_ndata(x,y,z) //got payload data in html page { alert(x); alert(y); alert(z); }

更多推荐

本文发布于:2023-07-31 01:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340538.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   Cordova   Phonegap   notification   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!