android - 通知 Android 后显示 Activity

标签 android notifications push-notification

我正在尝试在收到推送通知后打开 Activity 。

我收到了通知,但是当我选择它时没有任何反应!

问题轨迹是:

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 放入 list 中

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

哪里错了???

谢谢大家!!

编辑:

固定android:exported="true" 添加到我在 Manifest 中的 Activity

最佳答案

实现这个:

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

  }

关于android - 通知 Android 后显示 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22296415/

相关文章:

python - 在 post_save 上发送邀请电子邮件还是在 django View 中一次性发送邀请电子邮件?

ios - 如果我在聊天应用程序(WhatsApp 之类的应用程序)中使用 VOIP 功能,Apple 会拒绝我的应用程序吗

ios - ios 8 解析推送中没有声音

推送通知的 iOS 设备 token 示例

android - 带有文字说明的无限滚动画廊 View

java - 无法在 Facebook 中使用 WebviewHitResults 抓取 webview 中的图像 URL

android - 样式属性在 webview 中不起作用

android - flutter | Android Gradle 插件仅支持 Kotlin Gradle 插件版本 1.3.0 及更高版本

java - 有没有什么方法可以让通知始终可见,直到用户与之交互?

iphone - 当用户将文件拖到 iTunes 文件共享中并且同步完成时,有没有办法得到通知?