【问题标题】:show activity after notification Android通知Android后显示活动
【发布时间】:2014-03-10 09:31:54
【问题描述】:

我正在尝试在收到 PUSH 通知后打开一个活动。

我收到通知,但是当我选择它时,什么也没有发生!

问题追踪是:

W/InputMethodManagerService(771): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@438ae618 attribute=null, token = android.os.BinderProxy@4319aab8

这是我的代码

public class GCMIntentService extends IntentService {

private static final int NOTIF_ALERTA_ID = 1;

public GCMIntentService() {
    super("GCMIntentService");
}

@Override
protected void onHandleIntent(Intent intent)
{
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    String messageType = gcm.getMessageType(intent);
    Bundle extras = intent.getExtras();

    if (!extras.isEmpty())
    {
        if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType))
        {
            mostrarNotification(extras.getString("message"));
        }
    }

    GCMBroadcastReceiver.completeWakefulIntent(intent);
}

private void mostrarNotification(String msg)
{
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Intent notIntent = new Intent(this, OpenByNotificationActivity.class);
    PendingIntent contIntent = PendingIntent.getActivity(this, 1, notIntent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(android.R.drawable.stat_sys_warning)
    .setContentTitle("Notificación AppMovil")
    .setContentText(msg)
    .setContentIntent(contIntent);

    mNotificationManager.notify(NOTIF_ALERTA_ID, mBuilder.build());
}

我已经将我的活动放在清单中

<activity android:name="es.blabla.appmovil.activity.OpenByNotificationActivity" >
    </activity>

错在哪里???

谢谢大家!!

编辑:

已修复android:exported="true" 添加到我在清单中的活动中

【问题讨论】:

  • 试试这个:PendingIntent contIntent = PendingIntent.getActivity(this, 0, notIntent, 0);
  • 第二个你需要在创建Intent notIntent时传递Context
  • 已修复!!在 Manifest 中将 android:exported="true" 添加到我的活动中

标签: android notifications push-notification


【解决方案1】:

实现这个:

private void generateNotification(Context context, String message) {

int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, myactivity.class), 0);


 NotificationCompat.Builder builder = new NotificationCompat.Builder(
 context);
 notification = builder.setContentIntent(contentIntent)
 .setSmallIcon(icon).setTicker(appname).setWhen(0)
 .setAutoCancel(true).setContentTitle(appname)
 .setContentText(message).build();

 notificationManager.notify(0 , notification);

  }

【讨论】:

  • @SV 你说对了,但是有一个问题用户这样做到Service,你需要在new Intent(context, myactivity.class) 中传递上下文,那么这怎么可能?
  • 所以基本上,当您调用 GCMIntentService() 时,您可以在其中传递上下文参数并在其中分配 this.context。
  • @SV 这就是我所说的。为您的 Awesome asnwer +1。
  • 不起作用...我正在使用generateNotification(getApplicationContext(), extras.getString("message"));调用该方法
  • 你是否在 GCMIntentService() 中传递上下文?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多