android - IntentService 使用 NativeScript 和 Angular 处理 FCM
问题描述
我想使用 NativeScript + Angular 应用程序创建一个 IntentService,以便该服务在应用程序未运行时生成 FCM 通知。这是我在 Java 中为使用 GCM 推送通知的同一应用程序提供的一些代码。
public class MyBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setClass(context, MyService.class)));
setResultCode(Activity.RESULT_OK);
}}
这是我的 IntentService 的样子
public class MyIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public static final String TAG = "GCM";
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
/*
* Filter messages based on message type. Since it is likely that GCM will be
* extended in the future with new message types, just ignore any message types you're
* not interested in, or that you don't recognize.
*/
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " + extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// Post notification of received message.
String message = intent.getStringExtra("Sample");
sendNotification(message);
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
MyBroadcastReceiver.completeWakefulIntent(intent);
}
// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Log.i(TAG, "Inside SendNotification");
try {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(Constants.MESSAGE, msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
String[] lines = msg.split(System.getProperty("line.separator"));
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.store_icon)
.setContentTitle("Sample Notification")
.setAutoCancel(true)
.setContentText("Text");
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
mBuilder.setVibrate(null);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
} catch (Exception e) {
Log.i(TAG, e.getMessage());
}
}
}
可以使用 NativeScript 实现类似的代码吗?有可用的样品吗?
解决方案
推荐阅读
- javascript - 切片 JavaScript 对象
- angular - 如果 Observable 在加载后没有项目,则显示消息
- openshift - Openshift/OKD - 如何模板化 docker 机密
- rest - REST GET API 中的 OAuth2 安全性
- mysql - 从 .csv 文件加载表格数据的最快方法?
- java - 了解 Kotlin 访问修饰符
- c# - Nuget 引用错误,找不到命名空间
- react-native - componentdidmount() 中的反应原生状态问题
- google-analytics - Google Analytics API 中历史日期的有效维度组合没有返回数据是什么意思?
- elasticsearch - 用于命名实体链接的 Dbpedia 索引(聊天机器人)