通知不会打开活动

时间:2015-09-01 13:15:17

标签: android service notifications push-notification

我从服务“generateNotification”调用但在点击通知活动后没有打开:

来自服务:generateNotification(getApplicationContext(),contentTitle,contentText,contentText,contentTitle,contentTitle);

服务中的

方法:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void createNotificationAPI15(Context context, String title, String message,  String ticker_message, String summary_message, String big_message) {
         int id = (new Random()).nextInt(150) + 1;
        mNotificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);


    Intent notificationIntent = new Intent(context, ListActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

    stackBuilder.addNextIntent(notificationIntent);
    PendingIntent contentIntent = stackBuilder
            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
                    | PendingIntent.FLAG_ONE_SHOT);

    Notification notif  = new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.index2)
    .setContentIntent(contentIntent)
    .setTicker(ticker_message)
    .setContentTitle(title)
    .setContentText(message)
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(big_message))
    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.index2))
    .setAutoCancel(true)
    .build();

    mNotificationManager.notify(id,  notif);
}
中的

<uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="22" />
....
 <activity
            android:name=".ListActivity"
            android:exported="true"
            android:label="@string/list_act_name" >
        </activity>

3 个答案:

答案 0 :(得分:0)

像这样创建你的PendingIntent(它对我有用):

Intent intent = new Intent(context, ListActivity.class);
// optional flags, such as:
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);

确保将ListActivity.class传递给intent而不是android.app.ListActivity.class!

答案 1 :(得分:0)

需要进口:

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

答案 2 :(得分:0)

试试这个。

 Intent notificationIntent = new Intent(yourcurrentclass.this, ListActivity.class);
 PendingIntent contentIntent = PendingIntent.getActivity(yourcurrentclass.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);