java - Amazon SNS推送通知服务

标签 java android push-notification google-cloud-messaging amazon-sns

未传递注册ID。因此,我无法继续推送消息。此源代码直接来自Amazon SNS推送通知服务.TRY CATCH异常中是否有错误?

 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;
}

}

最佳答案

似乎register()方法中的AsyncTask没有被执行。试试下面,

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);

如果这不起作用,请尝试不使用AsyncTask进行注册。只是检查一下,在其他任何线程上的主线程中执行操作即可。

关于java - Amazon SNS推送通知服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25395942/

相关文章:

java - pbkdf2 加盐哈希密码验证

java - 有没有办法从文件中读取值并初始化数组?

java - 异步任务: Pass an exception value from doInBackground to onPostExecute

Android 应用更新返回缓存的应用更新信息

ios - 如何在ios上获取所有收到的通知

android - GCM 是否在后台运行?

java - 通过套接字将图像从 Android 发送到 PC

java - 基于 vector 和点的四元数旋转

android - 错误 :(42, 13) 无法解析 : com. android.support :appcompat-v7:26. 0.1

android - 使用 Urban Airship 库的 GCM 推送通知