移动后端启动器通知警报

时间:2014-10-11 00:39:59

标签: android mobile cloud backend

移动后端入门示例留言板不提供通知提醒, 怎么能实现呢? 试图修改GcmIntentService()但到目前为止没有成功?

1 个答案:

答案 0 :(得分:0)

这是我在GcmIntentService()中的代码,对我有用

@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)) {
            Log.i(Consts.TAG, "onHandleIntent: message error");
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            Log.i(Consts.TAG, "onHandleIntent: message deleted");
        // If it's a regular GCM message, do some work.
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType))) {
            String subId = intent.getStringExtra(GCM_KEY_SUBID);
            Log.e(Consts.TAG, "onHandleIntent: subId: " + subId);
            String[] tokens = subId.split(":");
            String typeId = tokens[1];

            // dispatch message
            if (GCM_TYPEID_QUERY.equals(typeId)) {
                Intent messageIntent = new Intent(BROADCAST_ON_MESSAGE);
                messageIntent.putExtras(intent);
                messageIntent.putExtra("token", tokens[2]);
                boolean isReceived = LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent);
                //Check if the broadcast has been handled, if not show the notification.
                if (!isReceived) {
                    Log.e(Consts.TAG, "A message has been recieved but no broadcast was handled.");
                    generateNotification();
                } else {
                    Log.e(Consts.TAG, "A message has been recieved, broadcasted and handled.");
                }
            }
        }
    }
    // Release the wake lock provided by the WakefulBroadcastReceiver.
    GCMBroadcastReceiver.completeWakefulIntent(intent);
}

public void generateNotification(){
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.notification)
                    .setContentTitle("A new notification")
                    .setContentText("Some text explaining the notification");

    // all the gubbins needed to make the onclick of the notification open the main activity
    Intent resultIntent = new Intent();
    resultIntent.setAction("com.company.projectname.action.LAUNCH_IT");
    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    this,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    // link the above onclick with the notification
    mBuilder.setContentIntent(resultPendingIntent);


    // Sets an ID for the notification
    int mNotificationId = 001;
    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Builds the notification and issues it.
    mNotifyMgr.notify(mNotificationId, mBuilder.build());
};