2016-07-23 4 views
0

通知をプルダウンしてその通知をクリックすると、そのアクティビティに電話したいと思います...どうすればいいですか?通知有効にする

はここに私のコードです:

public class SetReminder extends AppCompatActivity { 
int notifyID = 1088; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_notification); 

    TextView helpsubtitle = (TextView)findViewById(R.id.subtitle_help); 
    Typeface typeface = Typeface.createFromAsset(getAssets(), "beyond_the_mountains.ttf"); 
    helpsubtitle.setTypeface(typeface); 

    NotificationCompat.Builder mBuilder = 

      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.diamond) 
        .setContentTitle("Crystallise") 
        .setContentText("making your thoughts crystal clear"); 
    NotificationManager mNotificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(notifyID, mBuilder.build()); 
} 
} 

答えて

1

まず、あなたは、ユーザーが通知をクリックしたときに起動するアクティビティを指定Intentを作成する必要があります。あなたはNotificationManagerに与えるトークンをあるPendingIntent、または任意の他の外国出願を指定する必要があり、その後
Intent intent = new Intent(SetReminder.this, MainActivity.class);

:あなたは、通知をクリックしたときに開くようにMainActivityをしたいと仮定すると、意図を作成するために、次のコードを使用します一般的には:

PendingIntent pendingIntent = PendingIntent.getActivity(
     context, 0, 
     intent, PendingIntent.FLAG_CANCEL_CURRENT 
); 

.setContentIntent(pendingIntent)を使用して、あなたの通知にこのpendingIntentを適用します。

あなたの最終的なコードは次のようになります。

public class SetReminder extends AppCompatActivity { 
    int notifyID = 1088; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_notification); 

     TextView helpsubtitle = (TextView)findViewById(R.id.subtitle_help); 
     Typeface typeface = Typeface.createFromAsset(getAssets(), "beyond_the_mountains.ttf"); 
helpsubtitle.setTypeface(typeface); 

     Intent intent = new Intent(SetReminder.this, MainActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(
       context, 0, 
       intent, PendingIntent.FLAG_CANCEL_CURRENT 
     ); 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.diamond) 
       .setContentIntent(pendingIntent) 
       .setContentTitle("Crystallise") 
       .setContentText("making your thoughts crystal clear"); 
     NotificationManager mNotificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(notifyID, mBuilder.build()); 
    } 
} 
+0

鉱山シンボルコンテキスト+ asc42あなたはコンテキストを渡す必要が –

+0

を解決することはできませんと言います。 すでにアクティビティに参加しているアクティビティ自体から通知を作成しているので、 'context'を' SetReminder.this'に置き換える必要があります – adhirajsinghchauhan

関連する問題