简体   繁体   中英

Amazon SNS Push Notification Service

The Registration ID is not being passed. Thus, i cannot proceed to push the message. This Source code is directly taken from Amazon SNS Push Notification Service.Is there an error in TRY CATCH exception?

 public class MessageReceivingService extends Service{
     private GoogleCloudMessaging gcm;
     public static SharedPreferences savedValues;

 public static void sendToApp(Bundle extras, Context context){

     Intent newIntent = new Intent();
     newIntent.setClass(context, AndroidMobilePushApp.class);
     newIntent.putExtras(extras);
     newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     context.startActivity(newIntent);
}


public void onCreate(){

    super.onCreate();
    final String preferences = getString(R.string.preferences);
    savedValues = getSharedPreferences(preferences, Context.MODE_PRIVATE);
    // In later versions multi_process is no longer the default

     if(VERSION.SDK_INT >  9){
         savedValues = getSharedPreferences(preferences, Context.MODE_MULTI_PROCESS);
    }


     gcm = GoogleCloudMessaging.getInstance(getBaseContext());
     SharedPreferences savedValues =     PreferenceManager.getDefaultSharedPreferences(this);


     SharedPreferences.Editor editor1 =      savedValues.edit();
     editor1.putBoolean(getString(R.string.first_launch), true);
     editor1.commit();


    if(savedValues.getBoolean(getString(R.string.first_launch), true)){
        register();

        SharedPreferences.Editor editor = savedValues.edit();
        editor.putBoolean(getString(R.string.first_launch), false);
        editor.commit();
    }
    // Let AndroidMobilePushApp know we have just initialized and there may be stored   messages
    sendToApp(new Bundle(), this);
}

protected static void saveToLog(Bundle extras, Context context){
    SharedPreferences.Editor editor=savedValues.edit();
    String numOfMissedMessages = context.getString(R.string.num_of_missed_messages);
    int linesOfMessageCount = 0;
    for(String key : extras.keySet()){
        String line = String.format("%s=%s", key, extras.getString(key));
        editor.putString("MessageLine" + linesOfMessageCount, line);
        linesOfMessageCount++;
    }
    editor.putInt(context.getString(R.string.lines_of_message_count),  linesOfMessageCount);
    editor.putInt(context.getString(R.string.lines_of_message_count), linesOfMessageCount);
    editor.putInt(numOfMissedMessages, savedValues.getInt(numOfMissedMessages, 0) + 1);
    editor.commit();
    postNotification(new Intent(context, AndroidMobilePushApp.class), context);
}

protected static void postNotification(Intent intentAction, Context context){
    final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentAction, Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL);
    final Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Message Received!")
            .setContentText("")
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .getNotification();

    mNotificationManager.notify(R.string.notification_number, notification);
}

private void register() {

    new AsyncTask(){

        protected Object doInBackground(final Object... params) {

            String token;

              try {
                token = gcm.register(getString(R.string.project_number));
                Log.i("registrationId", token);
            } 
            catch (IOException e) {
                Log.i("Registration Error", "mesgerror " + e.getMessage());
            }
            return true;
        }
    }.execute(null, null, null);
}

public IBinder onBind(Intent arg0) {

   return null;
}

}

It seems that AsyncTask in register() method is not being executed. Try below,

new AsyncTask<Object, Object, Object>() { // Specify proper arguments
            protected Object doInBackground(final Object... params) {
                String token;
                try {
                    token = gcm.register(getString(R.string.project_number));
                    Log.i("registrationId", token);
                } catch (IOException e) {
                    Log.i("Registration Error", "mesgerror " + e.getMessage());
                }
                return true;
            }
        }.execute(null, null, null);

If this does not work, try registering without using AsyncTask. Just to check, do in on Main thread on any other thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM